

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 answer must be an even number and it can't be less than the square root of 269,696.
    So, if we start from that, keep on adding 2 and squaring it we'll eventually find the answer.
*/
 
import "/fmt" for Fmt                // this enables us to format numbers with thousand separators
 
var start = 269696.sqrt.ceil         // get the next integer higher than (or equal to) the square root
start = (start/2).ceil * 2           // if it's odd, use the next even integer
var i = start                        // assign it to a variable 'i' for use in the following loop
while (true) {                       // loop indefinitely till we find the answer
    var sq = i * i                   // get the square of 'i'
    var last6 = sq % 1000000         // get its last 6 digits by taking the remainder after division by a million
    if (last6 == 269696) {           // if those digits are 269696, we're done and can print the result
        Fmt.print("The lowest number whose square ends in 269,696 is $,d.", i)
        Fmt.print("Its square is $,d.", sq)
        break                        // break from the loop and end the program
    }
    i = i + 2                        // increase 'i' by 2
}