Magic constant: Difference between revisions

→‎{{header|AWK}}: Added Algol 68
(Added Sidef)
(→‎{{header|AWK}}: Added Algol 68)
Line 38:
 
 
 
=={{header|ALGOL 68}}==
{{Trans|FreeBasic}}
... with the inverse magic constant routine as in the Julia/Wren samples.
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses ALGOL 68G's LONG LOMG INT whose default precision is large enough to cope with 10^20.
<lang algol68>BEGIN # find some magic constants - the row, column and diagonal sums of a magin square #
# translation of the Free Basic sample with the Julia inverse function #
# returns the magic constant of a magic square of order n #
PROC a = ( INT n )LONG LONG INT:
BEGIN
LONG LONG INT n2 = n + 2;
( n2 * ( ( n2 * n2 ) + 1 ) ) OVER 2
END # a # ;
# returns the order of the magic square whose magic constant is at least x #
PROC inv a = ( LONG LONG INT x )LONG LONG INT:
ENTIER long long exp( long long ln( x * 2 ) / 3 ) + 1;
 
print( ( "The first 20 magic constants are " ) );
FOR n TO 20 DO
print( ( whole( a( n ), 0 ), " " ) )
OD;
print( ( newline ) );
print( ( "The 1,000th magic constant is ", whole( a( 1000 ), 0 ), newline ) );
LONG LONG INT e := 1;
FOR n TO 20 DO
e *:= 10;
print( ( "10^", whole( n, -2 ), ": ", whole( inv a( e ), -9 ), newline ) )
OD
END</lang>
{{out}}
<pre>
The first 20 magic constants are 15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
The 1,000th magic constant is 503006505
10^ 1: 3
10^ 2: 6
10^ 3: 13
10^ 4: 28
10^ 5: 59
10^ 6: 126
10^ 7: 272
10^ 8: 585
10^ 9: 1260
10^10: 2715
10^11: 5849
10^12: 12600
10^13: 27145
10^14: 58481
10^15: 125993
10^16: 271442
10^17: 584804
10^18: 1259922
10^19: 2714418
10^20: 5848036
</pre>
 
=={{header|AWK}}==
Line 91 ⟶ 146:
10^20: 5848036
</pre>
 
=={{header|Basic}}==
==={{header|FreeBASIC}}===
3,043

edits