

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.




' version 25-10-2016
' compile with: fbc -s console
 
' Charles Babbage would have known that only number ending
' on a 4 or 6 could produce a square ending on a 6
' also any number below 520 would produce a square smaller than 269,696
' we can stop when we have reached 99,736
' we know it square and it ends on 269,696
 
Dim As ULong number = 524 ' first number to try
Dim As ULong square, count
 
Do
    ' square the number
    square = number * number
    ' look at the last 6 digits, if they match print the number
    If Right(Str(square), 6) = "269696" Then Exit Do
    ' increase the number with 2, number end ons a 6
    number = number +2
    ' if the number = 99736 then we haved found a smaller number, so stop
    If number = 99736 Then Exit Do
    square = number * number
    ' look at the last 6 digits, if they match print the number
    If Right(Str(square),6 ) = "269696" Then Exit Do
    ' increase the number with 8, number ends on a 4
    number = number +8
    ' go to the first line under "Do"
Loop
 
If number = 99736 Then
    Print "No smaller number was found"
Else
    ' we found a smaller number, print the number and its square
    Print Using "The number = #####, and its square = ##########,"; number; square
End If
 
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End