Steady squares: Difference between revisions

Added Oberon-07
(Added Quackery.)
(Added Oberon-07)
 
(9 intermediate revisions by 3 users not shown)
Line 4:
<br>
The 3-digit number 376 in the decimal numbering system is an example of numbers with the special property that its square ends with the same digits: '''376*376 = 141376'''. Let's call a number with this property a steady square. Find steady squares under '''10.000'''
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">
HOW TO REPORT n is.steady.square.below power.of.ten:
REPORT ( n * n ) mod power.of.ten = n
 
HOW TO MIGHT n BE A STEADY SQUARE BELOW power.of.ten:
IF n is.steady.square.below power.of.ten:
WRITE ( ( ( n << 1 ) ^ "^2 = " ) >> 10 ) ^ ( ( n * n ) >> 1 ) /
 
PUT 10 IN power.of.ten
PUT -10 IN n
FOR i IN { 0 .. 1000 }:
PUT n + 10 IN n
IF n = power.of.ten:
PUT power.of.ten * 10 IN power.of.ten
MIGHT ( n + 1 ) BE A STEADY SQUARE BELOW power.of.ten
MIGHT ( n + 5 ) BE A STEADY SQUARE BELOW power.of.ten
MIGHT ( n + 6 ) BE A STEADY SQUARE BELOW power.of.ten
</syntaxhighlight>
{{out}}
<pre>
1^2 = 1
5^2 = 25
6^2 = 36
25^2 = 625
76^2 = 5776
376^2 = 141376
625^2 = 390625
9376^2 = 87909376
</pre>
 
=={{header|Action!}}==
Line 786 ⟶ 817:
<br>
Everything is Euler is an expression (apart from new/label/formal) and returns a value (although the value of a "goto" can't be used), so the "else" part of an "if" is not optional, hence the "else 0"s appearing in the code below.
'''begin'''
<syntaxhighlight lang="euler">
'''new''' maxNumber; '''new''' powerOfTen; '''new''' lastDigit; '''new''' n;
begin
'''new''' while;
new maxNumber; new powerOfTen; new lastDigit; new n;
new while;
while &lt;- ` '''formal''' condition; '''formal''' loopBody;
 
'''begin'''
while <- ` formal condition; formal loopBody;
begin '''label''' again;
again: '''if''' condition '''then''' '''begin''' loopBody; '''goto''' again '''end''' '''else''' 0
label again;
again: if condition then begin loopBody; goto again '''end else 0'''
end&apos;
';
;
maxNumber &lt;- 10 000;
 
maxNumber powerOfTen <&lt;- 10 000;
lastDigit &lt;- ( 1, 5, 6 );
powerOfTen <- 10;
lastDigit n <&lt;- ( 1, 5, 6 )-10;
while( ` [ n &lt;- n + 10 ] &lt;= maxNumber &apos;
n <- -10;
while( ` [ n <- n +, 10 ] <= maxNumber` '''begin'''
, ` begin '''new''' d;
'''if''' n = powerOfTen '''then''' powerOfTen &lt;- powerOfTen * 10
new d;
if n = powerOfTen then powerOfTen <- powerOfTen * 10 '''else''' 0;
d else&lt;- 0;
while( ` [ d <&lt;- 0d + 1 ] &lt;= '''length''' lastDigit &apos;
while( ` [ d <- d +, 1 ] <= length lastDigit` '''begin'''
, ` begin '''new''' nd; '''new''' n2;
new nd &lt;- n + lastDigit[ newd n2];
nd <n2 &lt;- nnd +* lastDigit[ d ]nd;
'''if''' n2 <-'''mod''' powerOfTen = nd *'''then''' '''out''' nd; '''else''' 0
if n2 mod powerOfTen = nd then out nd else 0'''end'''
end&apos;
')
)'''end'''
end&apos;
')
'''end'''
)
$
end
$
</syntaxhighlight>
{{out}}
<pre>
Line 1,139 ⟶ 1,168:
625 -> 390625
9376 -> 87909376</pre>
 
=={{header|Haxe}}==
<syntaxhighlight lang="haxe">
class Main // steady squares
{
static inline var MAX_NUMBER = 10000;
 
static function main()
{
var powerOfTen = 10;
var pad = ' ';
var lastDigit = [ 1, 5, 6 ]; // possible final digits
var n = -10;
for ( n10 in 0...Math.floor( MAX_NUMBER / 10 ) + 1 ) {
n += 10;
if( n == powerOfTen ) { // the number of digits just increased
powerOfTen *= 10;
pad = pad.substr( 1 );
}
for( d in 0...lastDigit.length ){
var nd = n + lastDigit[ d ];
var n2 = nd * nd;
if( n2 % powerOfTen == nd ){ // have a steady square
Sys.println( '$pad$nd^2 = $n2' );
}
}
}
}
 
}
</syntaxhighlight>
{{out}}
<pre>
1^2 = 1
5^2 = 25
6^2 = 36
25^2 = 625
76^2 = 5776
376^2 = 141376
625^2 = 390625
9376^2 = 87909376
</pre>\
 
=={{header|J}}==
Line 1,308 ⟶ 1,379:
 
 
 
=={{header|Lua}}==
{{Trans|ALGOL W}}
Uses the fact the final digit can only be 1, 5 or 6 (see the talk page).
<syntaxhighlight lang="lua">
do --[[ find steady squares - numbers whose square ends in the number
e.g.: 376^2 = 141 376
--]]
 
-- checks wheher n^2 mod p10 = n, i.e. n is a steady square and displays
-- a message if it is
local function possibleSteadySuare ( n, p10 )
if ( n * n ) % p10 == n then
io.write( string.format( "%4d", n ), "^2: ", n * n, "\n" )
end
end
 
local powerOfTen = 10
 
-- note the final digit must be 1, 5 or 6
for p = 0,10000,10 do
if p == powerOfTen then
-- number of digits has increased
powerOfTen = powerOfTen * 10
end
possibleSteadySuare( p + 1, powerOfTen )
possibleSteadySuare( p + 5, powerOfTen )
possibleSteadySuare( p + 6, powerOfTen )
end
end</syntaxhighlight>
{{out}}
<pre>
1^2: 1
5^2: 25
6^2: 36
25^2: 625
76^2: 5776
376^2: 141376
625^2: 390625
9376^2: 87909376
</pre>
 
=={{header|MAD}}==
Line 1,387 ⟶ 1,499:
625² = 390625
9376² = 87909376
</pre>
 
=={{header|Oberon-07}}==
<syntaxhighlight lang="modula2">
(* find some steady squares - numbers whose squares end in the number *)
(* e.g. 376^2 = 141 376 *)
 
MODULE SteadySquares;
IMPORT Out;
 
CONST maxNumber = 10000;
VAR n, powerOfTen :INTEGER;
 
PROCEDURE PossibleSteadySquare( r: INTEGER );
VAR r2: INTEGER;
BEGIN
r2 := r * r;
IF ( r2 MOD powerOfTen ) = r THEN
Out.Int( r, 6 );Out.String( "^2 = " );Out.Int( r2, 1 );Out.Ln
END
END PossibleSteadySquare;
 
BEGIN
powerOfTen := 10;
FOR n := 0 TO maxNumber BY 10 DO
IF n = powerOfTen THEN
(* the number of digits has increased *)
powerOfTen := powerOfTen * 10
END;
PossibleSteadySquare( n + 1 );
PossibleSteadySquare( n + 5 );
PossibleSteadySquare( n + 6 )
END
END SteadySquares.
</syntaxhighlight>
{{out}}
<pre>
1^2 = 1
5^2 = 25
6^2 = 36
25^2 = 625
76^2 = 5776
376^2 = 141376
625^2 = 390625
9376^2 = 87909376
</pre>
 
Line 2,141 ⟶ 2,298:
 
<pre>[ 0 1 5 6 25 76 376 625 9376 ]</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <FindSteady 1 9999>;
};
 
FindSteady {
s.N s.Max, <Compare s.N s.Max>: '+' = ;
s.N s.Max, <Steady <Symb s.N>>: F = <FindSteady <+ s.N 1> s.Max>;
s.N s.Max = <ShowSteady s.N> <FindSteady <+ s.N 1> s.Max>;
};
 
ShowSteady {
s.N = <Prout <Symb s.N> '^2 = ' <* s.N s.N>>;
};
 
Steady {
e.N, <Symb <* <Numb e.N> <Numb e.N>>>: e.X e.N = T;
e.N = F;
};</syntaxhighlight>
{{out}}
<pre>1^2 = 1
5^2 = 25
6^2 = 36
25^2 = 625
76^2 = 5776
376^2 = 141376
625^2 = 390625
9376^2 = 87909376</pre>
 
=={{header|REXX}}==
Line 2,342 ⟶ 2,528:
time(ms): 7
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program steady_squares;
loop for n in [1..10000] | steady n do
print(str n + "^2 = " + str (n**2));
end loop;
 
op steady(n);
return n**2 mod 10**#str n = n;
end op;
end program;</syntaxhighlight>
{{out}}
<pre>1^2 = 1
5^2 = 25
6^2 = 36
25^2 = 625
76^2 = 5776
376^2 = 141376
625^2 = 390625
9376^2 = 87909376</pre>
 
== {{header|TypeScript}} ==
Line 2,402 ⟶ 2,608:
{{libheader|Wren-fmt}}
Although it hardly matters for a small range such as this, one can cut down the numbers to be examined by observing that a steady square must end in 1, 5 or 6.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
System.print("Steady squares under 10,000:")
3,022

edits