Babbage problem: Difference between revisions
Content added Content deleted
(Created page with "{{draft task}} Charles Babbage, looking ahead to the sorts of problems his Analytical Engine would be able to solve, gave this example: what is the smallest number whose squar...") |
|||
Line 66: | Line 66: | ||
* the rest will not fit and will simply be discarded. |
* the rest will not fit and will simply be discarded. |
||
IF LAST-SIX IS EQUAL TO 269696 THEN DISPLAY N.</lang> |
IF LAST-SIX IS EQUAL TO 269696 THEN DISPLAY N.</lang> |
||
{{out}} |
|||
<pre>25264</pre> |
|||
=={{header|Processing}}== |
|||
<lang java>// Lines that begin with two slashes, thus, are comments: they |
|||
// will be ignored by the machine. |
|||
// First we must declare a variable, n, suitable to store an integer: |
|||
int n; |
|||
// Each statement we address to the machine must end with a semicolon. |
|||
// To begin with, the value of n will be zero: |
|||
n = 0; |
|||
// Now we must repeatedly increase it by one, checking each time to see |
|||
// whether its square ends in 269,696. |
|||
// We shall do this by seeing whether the remainder, when n squared |
|||
// is divided by one million, is equal to 269,696. |
|||
do { |
|||
n = n + 1; |
|||
} while (n * n % 1000000 != 269696); |
|||
// To read this formula, it is necessary to know the following |
|||
// elements of the notation: |
|||
// * means 'multiplied by' |
|||
// % means 'modulo', or remainder after division |
|||
// != means 'is not equal to' |
|||
// Now that we have our result, we need to display it. |
|||
// println is short for 'print line' |
|||
println(n);</lang> |
|||
{{out}} |
{{out}} |
||
<pre>25264</pre> |
<pre>25264</pre> |