Babbage problem: Difference between revisions

 
(7 intermediate revisions by 5 users not shown)
Line 383:
<p>Comentarios "in spanish":</p>
<p>
Documentación:</p>
<p>Establece que se trabajará con números sin decimales:</p>
"decimales(0)" (A)
Line 396:
La variable que controlará 11 iteraciones del proceso
principal. Se inicializa con 10, y se decrementará hasta
llegar a 0, de acuerdo a lo descrito en 1.4, 1.5 y 1.6:
 
"i" (BC)
 
Esto permitirá encontrar 11 números que cumplan con lo
con lo buscado.
 
-------------------------------------------------------------
Line 455:
Comentarios:
JNZ JNZ: significa Jump if Non-Zero.
 
#() : macro que permite resolver expresiones infijas en
tiempo de compilación.
 
</p>
Line 1,787 ⟶ 1,790:
=={{header|Elena}}==
{{trans|Smalltalk}}
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
import system'math;
Line 1,795 ⟶ 1,798:
var n := 1;
until(n.sqr().mod:(1000000) == 269696)
{
n += 1
Line 2,795 ⟶ 2,798:
{{out}}
<pre>{{x->25264},{x->99736}}</pre>
 
 
=={{header|Tiny Craft BasicMATLAB}}==
<syntaxhighlight lang="basicMATLAB">10 print "calculating..."
clear all;close all;clc;
BabbageProblem();
 
function BabbageProblem
% Initialize x to 524, as the square root of 269696 is approximately 519.something
x = 524;
% Loop until the square of x modulo 1000000 equals 269696
while mod(x^2, 1000000) ~= 269696
% If the last digit of x is 4, increment x by 2
% Otherwise, increment x by 8
if mod(x, 10) == 4
x = x + 2;
else
x = x + 8;
end
end
% Display the result
60 print " fprintf('The smallest numberpositive integer whose square ends in 269696 is:= "%d\n', nx);
end
</syntaxhighlight>
{{out}}
<pre>
The smallest positive integer whose square ends in 269696 = 25264
</pre>
 
=={{header|Maxima}}==
Line 4,050 ⟶ 4,083:
 
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
50 if (n ^ 2)if %r 1000000 <>= 269696.0 then 30
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
40 let 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}}==
Line 4,560 ⟶ 4,636:
638269696
</pre>
 
=={{header|Tiny Craft Basic}}==
<syntaxhighlight lang="basic">10 print "calculating..."
 
20 let n = 2
 
30 rem do
 
40 let n = n + 2
 
50 if (n ^ 2) % 1000000 <> 269696 then 30
 
60 print "The smallest number whose square ends in 269696 is: ", n
70 print "It's square is ", n * n</syntaxhighlight>
 
=={{header|Transd}}==
Line 4,918 ⟶ 4,980:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">/*
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.
Line 4,924 ⟶ 4,986:
*/
 
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
305

edits