Magic constant: Difference between revisions

m
Added Easylang
m (include other rosettacode magic square tasks in the See also list)
m (Added Easylang)
 
(22 intermediate revisions by 11 users not shown)
Line 41:
 
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F a(=n)
n += 2
R n * (n ^ 2 + 1) / 2
 
F inv_a(x)
V k = 0
L k * (k ^ 2.0 + 1) / 2 + 2 < x
k++
R k
 
print(‘The first 20 magic constants are:’)
L(n) 1..19
print(Int(a(n)), end' ‘ ’)
print("\nThe 1,000th magic constant is: "Int(a(1000)))
 
L(e) 1..19
print(‘10^’e‘: ’inv_a(10.0 ^ e))
</syntaxhighlight>
 
{{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
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
</pre>
 
=={{header|ALGOL 68}}==
Line 47 ⟶ 96:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses ALGOL 68G's LONG LONG INT whose default precision is large enough to cope with 10^20.
<langsyntaxhighlight 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/Wren inverse function #
# returns the magic constant of a magic square of order n + 2 #
Line 70 ⟶ 119:
print( ( "10^", whole( n, -2 ), ": ", whole( inv a( e ), -9 ), newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 99 ⟶ 148:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">a: function [n][
n: n+2
return (n*(1 + n^2))/2
Line 119 ⟶ 168:
print ""
loop 1..19 'z ->
print ["10 ^" z "=>" aInv 10^z]</langsyntaxhighlight>
 
{{out}}
Line 150 ⟶ 199:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MAGIC_CONSTANT.AWK
# converted from FreeBASIC
Line 175 ⟶ 224:
return(k)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 204 ⟶ 253:
=={{header|Basic}}==
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">
function a(byval n as uinteger) as ulongint
n+=2
Line 228 ⟶ 277:
for e as uinteger = 1 to 20
print using "10^##: #########";e;inv_a(10^cast(double,e))
next e</langsyntaxhighlight>
{{out}}<pre>
The first 20 magic constants are
Line 256 ⟶ 305:
 
==={{header|QB64}}===
<langsyntaxhighlight lang="qbasic">$NOPREFIX
 
DIM order AS INTEGER
Line 281 ⟶ 330:
FUNCTION MagicSum&& (n AS INTEGER)
MagicSum&& = (n * n + 1) / 2 * n
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre>
Line 309 ⟶ 358:
 
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">function a(n)
n = n + 2
return n*(n^2 + 1)/2
Line 332 ⟶ 381:
print "10^"; e; ": "; chr(9); inv_a(10^e)
next e
end</langsyntaxhighlight>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.i a(n.i)
n + 2
ProcedureReturn n*(Pow(n,2) + 1)/2
Line 359 ⟶ 408:
PrintN("10^" + Str(e) + ": " + #TAB$ + Str(inv_a(Pow(10,e))))
Next e
CloseConsole()</langsyntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight QBasiclang="qbasic">FUNCTION a (n)
n = n + 2
a = n * (n ^ 2 + 1) / 2
Line 387 ⟶ 436:
PRINT USING "10^##: #########"; e; inva(10 ^ e)
NEXT e
END</langsyntaxhighlight>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="qbasic">FUNCTION a(n)
LET n = n + 2
LET a = n*(n^2 + 1)/2
Line 414 ⟶ 463:
PRINT USING": #########": inv_a(10^e)
NEXT e
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">sub a(n)
n = n + 2
return n*(n^2 + 1)/2
Line 439 ⟶ 488:
print "10^", e using"##", ": ", inv_a(10^e) using "#########"
next e
end</langsyntaxhighlight>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
 
public class MagicConstant {
 
private const int OrderFirstMagicSquare = 3;
 
public static void Main(string[] args) {
Console.WriteLine("The first 20 magic constants:");
for (int i = 1; i <= 20; i++) {
Console.Write(" " + MagicConstantValue(Order(i)));
}
Console.WriteLine("\n");
 
Console.WriteLine("The 1,000th magic constant: " + MagicConstantValue(Order(1_000)) + "\n");
 
Console.WriteLine("Order of the smallest magic square whose constant is greater than:");
for (int i = 1; i <= 20; i++) {
string powerOf10 = "10^" + i + ":";
Console.WriteLine($"{powerOf10,6}{MinimumOrder(i),8}");
}
}
 
// Return the magic constant for a magic square of the given order
private static int MagicConstantValue(int n) {
return n * (n * n + 1) / 2;
}
 
// Return the smallest order of a magic square such that its magic constant is greater than 10 to the given power
private static int MinimumOrder(int n) {
return (int)Math.Exp((Math.Log(2.0)+ n * Math.Log(10.0)) / 3) + 1;
}
 
// Return the order of the magic square at the given index
private static int Order(int index) {
return OrderFirstMagicSquare + index - 1;
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 20 magic constants:
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: 503006505
 
Order of the smallest magic square whose constant is greater than:
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|C++}}==
<syntaxhighlight lang="c++">
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
 
constexpr int32_t ORDER_FIRST_MAGIC_SQUARE = 3;
constexpr double LN2 = log(2.0);
constexpr double LN10 = log(10.0);
 
// Return the magic constant for a magic square of the given order
int32_t magicConstant(int32_t n) {
return n * ( n * n + 1 ) / 2;
}
 
// Return the smallest order of a magic square such that its magic constant is greater than 10 to the given power
int32_t minimumOrder(int32_t n) {
return (int) exp( ( LN2 + n * LN10 ) / 3 ) + 1;
}
 
// Return the order of the magic square at the given index
int32_t order(int32_t index) {
return ORDER_FIRST_MAGIC_SQUARE + index - 1;
}
 
int main() {
std::cout << "The first 20 magic constants:" << std::endl;
for ( int32_t i = 1; i <= 20; ++i ) {
std::cout << " " << magicConstant(order(i));
}
std::cout << std::endl << std::endl;
 
std::cout << "The 1,000th magic constant: " << magicConstant(order(1'000)) << std::endl << std::endl;
 
std::cout << "Order of the smallest magic square whose constant is greater than:" << std::endl;
for ( int32_t i = 1; i <= 20; ++i ) {
std::string power_of_10 = "10^" + std::to_string(i) + ":";
std::cout << std::setw(6) << power_of_10 << std::setw(8) << minimumOrder(i) << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
The first 20 magic constants:
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: 503006505
 
Order of the smallest magic square whose constant is greater than:
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|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function GetMagicNumber(N: double): double;
begin
Result:=N * (((N * N) + 1) / 2);
end;
 
function GetNumberLess(N: double): integer;
var M: double;
begin
for Result:=1 to High(Integer) do
begin
M:=GetMagicNumber(Result);
if M>N then break;
end;
end;
 
procedure ShowMagicNumber(Memo: TMemo);
var I,J: integer;
var N,M: double;
var S: string;
begin
S:='';
for I:=3 to 23 do
begin
S:=S+Format('%8.0n',[GetMagicNumber(I)]);
if (I mod 5)=2 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('');
Memo.Lines.Add('1000th: '+Format('%8.0n',[GetMagicNumber(1002)]));
Memo.Lines.Add('');
N:=10;
for I:=1 to 20 do
begin
J:=GetNumberLess(N);
Memo.Lines.Add('M^'+Format('%d%8d',[I,J]));
N:=N * 10;
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
15 34 65 111 175
260 369 505 671 870
1,105 1,379 1,695 2,056 2,465
2,925 3,439 4,010 4,641 5,335
6,095
 
1000th: 503,006,505
 
M^1 3
M^2 6
M^3 13
M^4 28
M^5 59
M^6 126
M^7 272
M^8 585
M^9 1260
M^10 2715
M^11 5849
M^12 12600
M^13 27145
M^14 58481
M^15 125993
M^16 271442
M^17 584804
M^18 1259922
M^19 2714418
M^20 5848036
</pre>
 
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
func a n .
n += 2
return n * (n * n + 1) / 2
.
func inva x .
while k * (k * k + 1) / 2 + 2 < x
k += 1
.
return k
.
write "The first 20 magic constants: "
for n to 20
write a n & " "
.
print ""
print ""
print "The 1,000th magic constant: " & a 1000
print ""
print "Smallest magic square with constant greater than:"
for e to 10
print "10^" & e & ": " & inva pow 10 e
.
</syntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.functions.integer-logs
math.ranges prettyprint sequences ;
 
Line 460 ⟶ 761:
over integer-log10 over "10^%02d: %d\n" printf
dup + 1 -
] times 2drop</langsyntaxhighlight>
{{out}}
<pre>
Line 494 ⟶ 795:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 538 ⟶ 839:
fmt.Printf("10%-2s : %9s\n", superscript(i), rcu.Commatize(order))
}
}</langsyntaxhighlight>
 
{{out}}
<pre>
Same as Wren example.
</pre>
 
=={{header|J}}==
 
Implementation:<syntaxhighlight lang="j">mgc=: 0 0.5 0 0.5&p.</syntaxhighlight>
 
In other words, the magic constant for a magic square of order <tt>x</tt> is the result of the polynomial <code>(0.5*x)+(0.5*x^3)</code>
 
Task examples:<syntaxhighlight lang="j"> mgc 3+i.20
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
mgc 1003x
504514015
(#\,.],.mgc) x:(mgc i.3000) I.10^1+i.10
1 3 15
2 6 111
3 13 1105
4 28 10990
5 59 102719
6 126 1000251
7 272 10061960
8 585 100101105
9 1260 1000188630
10 2715 10006439295</syntaxhighlight>
stretch example:<syntaxhighlight lang="j"> ((10+#\),.],.mgc) x:(mgc i.6e6) I.10^11+i.10
11 5849 100049490449
12 12600 1000188006300
13 27145 10000910550385
14 58481 100003310078561
15 125993 1000021311323825
16 271442 10000026341777165
17 584804 100000232056567634
18 1259922 1000002262299152685
19 2714418 10000004237431278525
20 5848036 100000026858987459346</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public final class MagicConstant {
 
public static void main(String[] aArgs) {
System.out.println("The first 20 magic constants:");
for ( int i = 1; i <= 20; i++ ) {
System.out.print(" " + magicConstant(order(i)));
}
System.out.println(System.lineSeparator());
System.out.println("The 1,000th magic constant: " + magicConstant(order(1_000)) + System.lineSeparator());
System.out.println("Order of the smallest magic square whose constant is greater than:");
for ( int i = 1; i <= 20; i++ ) {
String powerOf10 = "10^" + i + ":";
System.out.println(String.format("%6s%8s", powerOf10, minimumOrder(i)));
}
}
// Return the magic constant for a magic square of the given order
private static int magicConstant(int aN) {
return aN * ( aN * aN + 1 ) / 2;
}
 
// Return the smallest order of a magic square such that its magic constant is greater than 10 to the given power
private static int minimumOrder(int aN) {
return (int) Math.exp( ( LN2 + aN * LN10 ) / 3 ) + 1;
}
// Return the order of the magic square at the given index
private static int order(int aIndex) {
return ORDER_FIRST_MAGIC_SQUARE + aIndex - 1;
}
private static final int ORDER_FIRST_MAGIC_SQUARE = 3;
private static final double LN2 = Math.log(2.0);
private static final double LN10 = Math.log(10.0);
 
}
</syntaxhighlight>
{{ out }}
<pre>
The first 20 magic constants:
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: 503006505
 
Order of the smallest magic square whose constant is greater than:
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>
 
Line 551 ⟶ 956:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq"># To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
Line 586 ⟶ 991:
elif . < 20 then ss[1] + ss[. - 10]
else ss[2] + ss[0]
end;</langsyntaxhighlight>
 
'''The Task'''
<syntaxhighlight lang="text">
def magicConstant: (.*. + 1) * . / 2;
Line 603 ⟶ 1,008:
| (10 | power($i)) as $goal
| ((($goal * 2)|iroot(3) + 1) | floor) as $order
| ("10\($i|superscript)" | lpad(5)) + ": \($order|lpad(9))" )</langsyntaxhighlight>
{{out}}
As for [[#Wren|Wren]] except for commatization.
Line 609 ⟶ 1,014:
=={{header|Julia}}==
Uses the inverse of the magic constant function for the last part of the task.
<langsyntaxhighlight lang="julia">using Lazy
 
magic(x) = (1 + x^2) * x ÷ 2
Line 622 ⟶ 1,027:
println("10^", string(expo, pad=2), " ", ordr)
end
</langsyntaxhighlight>{{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]
Line 648 ⟶ 1,053:
10^20 5848036
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function magic (x)
return x * (1 + x^2) / 2
end
 
print("Magic constants of orders 3 to 22:")
for i = 3, 22 do
io.write(magic(i) .. " ")
end
 
print("\n\nMagic constant 1003: " .. magic(1003) .. "\n")
 
print("Orders of smallest magic constant greater than...")
print("-----\t-----\nValue\tOrder\n-----\t-----")
local order = 1
for i = 1, 20 do
repeat
order = order + 1
until magic(order) > 10 ^ i
print("10^" .. i, order)
end</syntaxhighlight>
{{out}}
<pre>Magic constants of orders 3 to 22:
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
 
Magic constant 1003: 504514015
 
Orders of smallest magic constant greater than...
----- -----
Value Order
----- -----
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|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[i, n, MagicSumHelper, MagicSum, InverseMagicSum]
MagicSumHelper[n_] = Sum[i, {i, n^2}]/n;
MagicSum[n_] := MagicSumHelper[n + 2]
Line 660 ⟶ 1,117:
exps = Range[1, 50];
nums = 10^exps;
Transpose[{Superscript[10, #] & /@ exps, InverseMagicSum[nums]}] // Grid</langsyntaxhighlight>
{{out}}
<pre>{15, 34, 65, 111, 175, 260, 369, 505, 671, 870, 1105, 1379, 1695, 2056, 2465, 2925, 3439, 4010, 4641, 5335}
Line 715 ⟶ 1,172:
10^49 27144176165949066
10^50 58480354764257322</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[math, unicode]
 
func magicConstant(n: int): int =
## Return the magic constant for a magic square of order "n".
n * (n * n + 1) div 2
 
func minOrder(n: int): int =
## Return the smallest order such as the magic constant is greater than "10^n".
const Ln2 = ln(2.0)
const Ln10 = ln(10.0)
result = int(exp((Ln2 + n.toFloat * Ln10) / 3)) + 1
 
const First = 3
const Superscripts: array['0'..'9', string] = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"]
 
template order(idx: Positive): int =
## Compute the order of the magic square at index "idx".
idx + (First - 1)
 
func superscript(n: Natural): string =
## Return the Unicode string to use to represent an exponent.
for d in $n:
result.add(Superscripts[d])
 
echo "First 20 magic constants:"
for idx in 1..20:
stdout.write ' ', order(idx).magicConstant
echo()
 
echo "\n1000th magic constant: ", order(1000).magicConstant
 
echo "\nOrder of the smallest magic square whose constant is greater than:"
for n in 1..20:
let left = "10" & n.superscript & ':'
echo left.alignLeft(6), ($minOrder(n)).align(7)
</syntaxhighlight>
 
{{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
 
Order of the smallest magic square whose constant is greater than:
10¹: 3
10²: 6
10³: 13
10⁴: 28
10⁵: 59
10⁶: 126
10⁷: 272
10⁸: 585
10⁹: 1260
10¹⁰: 2715
10¹¹: 5849
10¹²: 12600
10¹³: 27145
10¹⁴: 58481
10¹⁵: 125993
10¹⁶: 271442
10¹⁷: 584804
10¹⁸: 1259922
10¹⁹: 2714418
10²⁰: 5848036
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
program MagicConst;
{$IFDEF FPC}{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
 
function MagicSum(n :Uint32):Uint64; inline;
var
k : Uint64;
begin
k := n*Uint64(n);
result := (k*k+k) DIV 2;
end;
 
function MagSumPerRow(n:Uint32):Uint32;
begin
//result := MagicSum(n) DIV n;
//(n^3 + n) /2
result := ((Uint64(n)*n+1)*n) DIV 2;
end;
var
s : String[31];
i : Uint32;
lmt,rowcnt : extended;
Begin
writeln('First Magic constants 3..20');
For i := 3 to 20 do
write(MagSumPerRow(i),' ');
writeln;
writeln('1000.th ',MagSumPerRow(1002));
 
writeln('First Magic constants > 10^xx');
//lmt = (rowcnt^3 + rowcnt) /2 -> rowcnt > (lmt*2 )^(1/3)
lmt := 2.0 * 10.0;
For i := 1 to 50 do
begin
rowcnt := Int(exp(ln(lmt)/3))+1.0;//+1 suffices
str(trunc(rowcnt),s);
writeln('10^',i:2,#9,s:18);
f := 10.0*lmt;
end;
end.</syntaxhighlight>
{{out}}
<pre>
First Magic constants 3..20
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010
1000.th 503006505
First Magic constants > 10^xx
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
... same as Mathematica
10^46 2714417616594907
10^47 5848035476425733
10^48 12599210498948732
10^49 27144176165949066
10^50 58480354764257322</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Magic_constant
Line 732 ⟶ 1,332:
{
printf "%3d %9d\n", $i, (10 ** $i * 2) ** ( 1 / 3 ) + 1;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 764 ⟶ 1,364:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 784 ⟶ 1,384:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1e%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 813 ⟶ 1,413:
=={{header|Prolog}}==
Minimalistic, efficient approach
<langsyntaxhighlight lang="prolog">
m(X,Y):- Y is X*(X*X+1)/2.
 
Line 824 ⟶ 1,424:
write("The 1000th magic constant is:"), forall(m(1002,X), format(" ~d",X)), nl,
forall(between(1,20,N), (l(10**N,X), format("10^~d:\t~d\n",[N,X]))).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 855 ⟶ 1,455:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def a(n):
Line 875 ⟶ 1,475:
for e in range(1, 20):
print(f'10^{e}: {inv_a(10**e)}');</langsyntaxhighlight>
{{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
The 1,000th magic constant is: 503006505.0
10^1: 3
10^2: 6
Line 899 ⟶ 1,499:
10^18: 1259922
10^19: 2714418</pre>
 
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery"> [ 3 + dup 3 ** + 2 / ] is magicconstant ( n --> n )
20 times [ i^ magicconstant echo sp ] cr cr
Line 915 ⟶ 1,514:
[ 10 21 ** ] constant
over = until ]
2drop</langsyntaxhighlight>
 
{{out}}
Line 947 ⟶ 1,546:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers:ver<2.8+>;
 
my @magic-constants = lazy (3..∞).hyper.map: { (1 + .²) * $_ / 2 };
Line 955 ⟶ 1,554:
say "\nSmallest order magic square with a constant greater than:";
 
(1..20).map: -> $p {printf "10%-2s: %s\n", $p.&super, comma 3 + @magic-constants.first( * > exp($p, 10), :k ) }</langsyntaxhighlight>
{{out}}
<pre>First 20 magic constants: 15 34 65 111 175 260 369 505 671 870 1,105 1,379 1,695 2,056 2,465 2,925 3,439 4,010 4,641 5,335
Line 981 ⟶ 1,580:
10¹⁹: 2,714,418
10²⁰: 5,848,036</pre>
 
=={{header|RPL}}==
This task can be solved through a few one-liners, by using algebraic and equation-solving features.
 
First, let's store the equation of a(n):
'X*(SQ(X)+1)/2' ''''A6003'''' STO
Then, evaluate it for n=3 to 22 to get the first 20 magic constants:
≪ {} 3 22 '''FOR''' j j 'X' STO '''A6003''' EVAL + '''NEXT''' ≫ EVAL
We need now to define the inverse function of a(n):
≪ '''A6003''' OVER - 'X' ROT ROOT CEIL ≫ ''''A6003→'''' STO
And finally, look for a<sup>-1</sup>( 10 ^ j ), for j=1 to 10:
≪ {} 1 10 '''FOR''' j 10 j ^ '''A6003→''' + '''NEXT''' ≫ EVAL
which is a one-minute job for a basic HP-28S.
{{out}}
<pre>
2: { 15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335 }
1: { 3 6 13 28 59 126 272 585 1260 2715 }
</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func f(n) {
(n+2) * ((n+2)**2 + 1) / 2
}
Line 996 ⟶ 1,613:
for n in (1 .. 20) {
printf("order(10^%-2s) = %s\n", n, order(10**n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,027 ⟶ 1,644:
{{libheader|Wren-fmt}}
This uses Julia's approach for the final parts.
<langsyntaxhighlight ecmascriptlang="wren">import "./seq" for Lst
import "./fmt" for Fmt
Line 1,048 ⟶ 1,665:
var order = (goal * 2).cbrt.floor + 1
Fmt.print("10$-2s : $,9d", superscript.call(i), order)
}</langsyntaxhighlight>
 
{{out}}
Line 1,086 ⟶ 1,703:
constant, and N rows add to the Sum. Thus the magic constant = Sum/N =
(N^3+N)/2.
<langsyntaxhighlight XPL0lang="xpl0">int N, X;
real M, Thresh, MC;
[Text(0, "First 20 magic constants:^M^J");
Line 1,112 ⟶ 1,729:
Thresh:= Thresh*10.;
];
]</langsyntaxhighlight>
 
{{out}}
1,980

edits