Babbage problem: Difference between revisions

Added S-BASIC example
imported>KayproKid
(Added S-BASIC example)
Line 4,053:
 
Which outputs the same thing as above.
 
=={{header|S-BASIC}}==
Because we have to use double-precision floating point to represent the required number of digits, and because the approach to calculating a double-precision n mod 1000000 to isolate the right-most six digits of the square is particularly inefficient, the program will take a long time to find the solution (but it will, eventually!)
<syntaxhighlight lang="BASIC">
$lines
 
$constant true = FFFFH
$constant false = 0
 
var n, sq, r = real.double
var done = integer
 
print "Finding smallest number whose square ends in 269696"
 
n = 520 rem - no smaller number has a square that large
done = false
 
rem - no need to search beyond the number Babbage already knew
while not done and n <= 99736.0 do
begin
sq = n * n
rem - compute sq mod 1000000 by repeated subtraction
r = sq
while r >= 1000000.0 do
r = r - 1000000.0
if r = 269696.0 then
begin
print using "The smallest number is ######"; n
print using "and its square is ##,###,###,###"; sq
done = true
end
rem - only even numbers can have a square ending in 6
n = n + 2
end
 
end
</syntaxhighlight>
{{out}}
<pre>
Finding smallest number whose square ends in 269696
The smallest number is 25264
and its square is 638,269,696
</pre>
 
=={{header|Scala}}==
Anonymous user