

Charles Babbage, looking ahead to the sorts of problems his Analytical Engine would be able to solve, gave this example:

 What is the smallest positive integer whose square ends in the digits 269,696?

— Babbage, letter to Lord Bowden, 1837; see Hollingdale and Tootill, Electronic Computers, second edition, 1970, p. 125.

He thought the answer might be 99,736, whose square is 9,947,269,696; but he couldn't be certain.



Task[edit]
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].



Motivation
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.




(   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   )
 
( The computer will ignore anything between parentheses, providing there is
  a space or carriage return on either side of each parenthesis.            )
 
(   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   )
 
( A number squared is that number, multiplied by itself.
 
  Here, "dup" means "make a duplicate" and "*" means "multiply two numbers",
  So "dup *" means "make a duplicate of a number and multiply it by itself".
  We will tell the computer that this action is called "squared".           )
 
 
                            [ dup * ] is squared                           
 
 
( "squared" takes a number and returns that number, squared.                )
 
(   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   )
 
( A number n ends with the digits 269696 if n mod 1000000 = 269696,
  When expressed in postfix notation this is: 1000000 mod 269696 =
  We will tell the computer that this test is called "ends.with.269696".    )
 
 
                [ 1000000 mod 269696 = ] is ends.with.269696
 
 
( "ends.with.269696" takes a number and returns true if it ends with 269696,
  and false if it does not.                                                 )
 
(   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   )
 
( The square root of 269696 is approximately equal to 518.35, so we need not
  consider numbers less than 519.
 
  Starting withe 518, we will intruct the computer to repeatedly add 2 to
  the number using the command "2 +" (because the number we are looking for 
  must be an even number as the square root of any even perfect square
  must be an even number), then square a duplicate of the number until a
  value is found of which its square ends with 269696.
 
  "until" will take the truth value returned by "ends.with.269696" and, if
  that value is "false", will cause the previous commands from the nearest
  "[" preceding "until" to be repeated.
 
  When the value is "true" the computer will proceed to the word "echo",
  which will take the calculated solution and display it on the screen.     )
 
 
             518 [ 2 + dup squared ends.with.269696 until ] echo
 
 
(   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   )