Magic constant: Difference between revisions

Added XPL0 example.
(→‎{{header|Wren}}: Now uses new BigInt cube root function rather than Num version - still quick (17 ms).)
(Added XPL0 example.)
Line 280:
10¹⁹ : 2,714,418
10²⁰ : 5,848,036
</pre>
 
=={{header|XPL0}}==
A magic square of side N contains N^2 items. The sum of a sequence 1..N^2
is given by: Sum = (N^2+1) * N^2 / 2. A grid row adds to the magic
constant, and N rows add to the Sum. Thus the magic constant = Sum/N =
(N^3+N)/2.
<lang XPL0>int N, X;
real M, Thresh, MC;
[Text(0, "First 20 magic constants:^M^J");
for N:= 3 to 20+3-1 do
[IntOut(0, (N*N*N+N)/2); ChOut(0, ^ )];
CrLf(0);
Text(0, "1000th magic constant: ");
N:= 1000+3-1;
IntOut(0, (N*N*N+N)/2);
CrLf(0);
Text(0, "Smallest order magic square with a constant greater than:^M^J");
Thresh:= 10.;
M:= 3.;
Format(1, 0);
for X:= 1 to 10 do
[repeat MC:= (M*M*M+M)/2.;
M:= M+1.;
until MC > Thresh;
Text(0, "10^^");
if X < 10 then ChOut(0, ^0);
IntOut(0, X);
Text(0, ": ");
RlOut(0, M-1.);
CrLf(0);
Thresh:= Thresh*10.;
];
]</lang>
 
{{out}}
<pre>
First 20 magic constants:
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
1000th magic constant: 503006505
Smallest order magic square with a constant greater than:
10^01: 3
10^02: 6
10^03: 13
10^04: 28
10^05: 59
10^06: 126
10^07: 272
10^08: 585
10^09: 1260
10^10: 2715
</pre>
772

edits