How to resolve the algorithm Babbage problem step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Babbage problem step by step in the REXX programming language

Table of Contents

Problem Statement

Charles Babbage, looking ahead to the sorts of problems his Analytical Engine would be able to solve, gave this example: He thought the answer might be 99,736, whose square is 9,947,269,696; but he couldn't be certain.

The task is to find out if Babbage had the right answer — and to do so, as far as your language allows it, in code that Babbage himself would have been able to read and understand. As Babbage evidently solved the task with pencil and paper, a similar efficient solution is preferred. For these purposes, Charles Babbage may be taken to be an intelligent person, familiar with mathematics and with the idea of a computer; he has written the first drafts of simple computer programmes in tabular form. [Babbage Archive Series L].

The aim of the task is to write a program that is sufficiently clear and well-documented for such a person to be able to read it and be confident that it does indeed solve the specified problem.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Babbage problem step by step in the REXX programming language

Source code in the rexx programming language

/*REXX program finds the  lowest (positive)  integer  whose  square  ends in  269,696.  */
   do j=2  by 2  until right(j * j, 6) == 269696 /*start  J  at two,  increment by two. */
   end                                           /*◄── signifies the end of the DO loop.*/
                                                 /* [↑]     *     means multiplication. */
say "The smallest integer whose square ends in  269,696  is: "    j


/*REXX program finds the  lowest (positive)  integer  whose  square  ends in  269,696.  */
   do j=2  by 2                                  /*start  J  at two,  increment by two. */
   if ((j * j) // 1000000) == 269696  then leave /*is square mod one million our target?*/
   end                                           /*◄── signifies the end of the DO loop.*/
                                                 /* [↑]    //     is division remainder.*/
say "The smallest integer whose square ends in  269,696  is: "    j


/*REXX program finds the  lowest (positive)  integer  whose  square  ends in  269,696.  */
/*─────────────────── we will only examine integers that are ending  in  four  or  six. */
   do j=4  by 10                                 /*start  J  at four,  increment by ten.*/
   k = j                                         /*set    K  to  J's  value.            */
   if right(k * k, 6) == 269696  then leave      /*examine right-most 6 decimal digits. */
                                                 /*      ==  means exactly equal to.    */
   k = j+2                                       /*set    K  to  J+2  value.            */
   if right(k * k, 6) == 269696  then leave      /*examine right-most 6 decimal digits. */
   end                                           /*◄── signifies the end of the DO loop.*/
                                                 /* [↑]      *    means multiplication. */
say "The smallest integer whose square ends in  269,696  is: "   k


/*REXX ----------------------------------------------------------------
* The solution must actually be larger than sqrt(269696)=519.585
*--------------------------------------------------------------------*/
z=0
Do i=524 By 10 Until z>0
  If right(i*i,6)==269696  then z=i
  Else Do
   j=i+2
   if right(j*j,6)==269696  then z=j
   End
 End
Say "The smallest integer whose square ends in 269696 is:" z
Say '                            'z'**2 =' z**2


  

You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the Wren programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the gnuplot programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the GUISS programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the Julia programming language
You may also check:How to resolve the algorithm Substring step by step in the NetRexx programming language