Babbage problem: Difference between revisions

Content added Content deleted
(Clarified that the task refers to positive integers)
Line 9: Line 9:
<lang bbcbasic>REM Statements beginning 'REM' are explanatory remarks: the machine will ignore them.
<lang bbcbasic>REM Statements beginning 'REM' are explanatory remarks: the machine will ignore them.


REM We shall test values of n from 1 upwards until we find one whose square ends in 269,696.
REM We shall test positive integers from 1 upwards until we find one whose square ends in 269,696.


REM A number that ends in 269,696 is one that leaves a remainder of 269,696 when divided by a million.
REM A number that ends in 269,696 is one that leaves a remainder of 269,696 when divided by a million.


REM So we are looking for a number that satisfies the condition 'n squared modulo 1,000,000 = 269,696', or 'n^2 MOD 1000000 = 269696' in the notation that the machine can accept.
REM So we are looking for a value of n that satisfies the condition 'n squared modulo 1,000,000 = 269,696', or 'n^2 MOD 1000000 = 269696' in the notation that the machine can accept.


REM Observe that in this notation groups of digits may not be separated by commas.
REM Observe that in this notation groups of digits may not be separated by commas.
Line 24: Line 24:


REM 'REPEAT... UNTIL' causes the machine to perform the addition repeatedly until the condition is satisfied.
REM 'REPEAT... UNTIL' causes the machine to perform the addition repeatedly until the condition is satisfied.

REM Although n is initially equal to 0, the first number that is actually tested is 1.

REM This is because the addition is carried out before the test.


PRINT "The smallest number whose square ends in 269696 is" n
PRINT "The smallest number whose square ends in 269696 is" n
PRINT "Its square is" n^2
PRINT "Its square is" n^2</lang>

REM 'PRINT' causes anything enclosed in double quotation marks to be displayed just as it is written.
REM The expressions 'n' and 'n^2' are not so enclosed: they are therefore evaluated, and what is displayed is the result.</lang>
{{out}}
{{out}}
<pre>The smallest number whose square ends in 269696 is 25264
<pre>The smallest number whose square ends in 269696 is 25264