Magic constant: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|J}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 47:
{{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.
<
# 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:
print( ( "10^", whole( n, -2 ), ": ", whole( inv a( e ), -9 ), newline ) )
OD
END</
{{out}}
<pre>
Line 99:
=={{header|Arturo}}==
<
n: n+2
return (n*(1 + n^2))/2
Line 119:
print ""
loop 1..19 'z ->
print ["10 ^" z "=>" aInv 10^z]</
{{out}}
Line 150:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MAGIC_CONSTANT.AWK
# converted from FreeBASIC
Line 175:
return(k)
}
</syntaxhighlight>
{{out}}
<pre>
Line 204:
=={{header|Basic}}==
==={{header|FreeBASIC}}===
<
function a(byval n as uinteger) as ulongint
n+=2
Line 228:
for e as uinteger = 1 to 20
print using "10^##: #########";e;inv_a(10^cast(double,e))
next e</
{{out}}<pre>
The first 20 magic constants are
Line 256:
==={{header|QB64}}===
<
DIM order AS INTEGER
Line 281:
FUNCTION MagicSum&& (n AS INTEGER)
MagicSum&& = (n * n + 1) / 2 * n
END FUNCTION</
{{out}}
<pre>
Line 309:
==={{header|BASIC256}}===
<
n = n + 2
return n*(n^2 + 1)/2
Line 332:
print "10^"; e; ": "; chr(9); inv_a(10^e)
next e
end</
==={{header|PureBasic}}===
<
n + 2
ProcedureReturn n*(Pow(n,2) + 1)/2
Line 359:
PrintN("10^" + Str(e) + ": " + #TAB$ + Str(inv_a(Pow(10,e))))
Next e
CloseConsole()</
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<
n = n + 2
a = n * (n ^ 2 + 1) / 2
Line 387:
PRINT USING "10^##: #########"; e; inva(10 ^ e)
NEXT e
END</
==={{header|True BASIC}}===
<
LET n = n + 2
LET a = n*(n^2 + 1)/2
Line 414:
PRINT USING": #########": inv_a(10^e)
NEXT e
END</
==={{header|Yabasic}}===
<
n = n + 2
return n*(n^2 + 1)/2
Line 439:
print "10^", e using"##", ": ", inv_a(10^e) using "#########"
next e
end</
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
math.ranges prettyprint sequences ;
Line 460:
over integer-log10 over "10^%02d: %d\n" printf
dup + 1 -
] times 2drop</
{{out}}
<pre>
Line 494:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 538:
fmt.Printf("10%-2s : %9s\n", superscript(i), rcu.Commatize(order))
}
}</
{{out}}
Line 547:
=={{header|J}}==
Implementation:<
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:<
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
mgc 1003x
Line 565:
8 585 100101105
9 1260 1000188630
10 2715 10006439295</
stretch example:<
11 5849 100049490449
12 12600 1000188006300
Line 576:
18 1259922 1000002262299152685
19 2714418 10000004237431278525
20 5848036 100000026858987459346</
=={{header|jq}}==
Line 584:
'''Preliminaries'''
<
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
Line 619:
elif . < 20 then ss[1] + ss[. - 10]
else ss[2] + ss[0]
end;</
'''The Task'''
<syntaxhighlight lang="text">
def magicConstant: (.*. + 1) * . / 2;
Line 636:
| (10 | power($i)) as $goal
| ((($goal * 2)|iroot(3) + 1) | floor) as $order
| ("10\($i|superscript)" | lpad(5)) + ": \($order|lpad(9))" )</
{{out}}
As for [[#Wren|Wren]] except for commatization.
Line 642:
=={{header|Julia}}==
Uses the inverse of the magic constant function for the last part of the task.
<
magic(x) = (1 + x^2) * x ÷ 2
Line 655:
println("10^", string(expo, pad=2), " ", ordr)
end
</
<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 683:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
MagicSumHelper[n_] = Sum[i, {i, n^2}]/n;
MagicSum[n_] := MagicSumHelper[n + 2]
Line 693:
exps = Range[1, 50];
nums = 10^exps;
Transpose[{Superscript[10, #] & /@ exps, InverseMagicSum[nums]}] // Grid</
{{out}}
<pre>{15, 34, 65, 111, 175, 260, 369, 505, 671, 870, 1105, 1379, 1695, 2056, 2465, 2925, 3439, 4010, 4641, 5335}
Line 750:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Magic_constant
Line 765:
{
printf "%3d %9d\n", $i, (10 ** $i * 2) ** ( 1 / 3 ) + 1;
}</
{{out}}
<pre>
Line 797:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 817:
<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>
<!--</
{{out}}
<pre>
Line 846:
=={{header|Prolog}}==
Minimalistic, efficient approach
<
m(X,Y):- Y is X*(X*X+1)/2.
Line 857:
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>
{{out}}
<pre>
Line 888:
=={{header|Python}}==
<
def a(n):
Line 908:
for e in range(1, 20):
print(f'10^{e}: {inv_a(10**e)}');</
{{out}}
<pre>The first 20 magic constants are:
Line 935:
=={{header|Quackery}}==
<
20 times [ i^ magicconstant echo sp ] cr cr
Line 948:
[ 10 21 ** ] constant
over = until ]
2drop</
{{out}}
Line 980:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my @magic-constants = lazy (3..∞).hyper.map: { (1 + .²) * $_ / 2 };
Line 988:
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 ) }</
{{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 1,016:
=={{header|Sidef}}==
<
(n+2) * ((n+2)**2 + 1) / 2
}
Line 1,029:
for n in (1 .. 20) {
printf("order(10^%-2s) = %s\n", n, order(10**n))
}</
{{out}}
<pre>
Line 1,060:
{{libheader|Wren-fmt}}
This uses Julia's approach for the final parts.
<
import "./fmt" for Fmt
Line 1,081:
var order = (goal * 2).cbrt.floor + 1
Fmt.print("10$-2s : $,9d", superscript.call(i), order)
}</
{{out}}
Line 1,119:
constant, and N rows add to the Sum. Thus the magic constant = Sum/N =
(N^3+N)/2.
<
real M, Thresh, MC;
[Text(0, "First 20 magic constants:^M^J");
Line 1,145:
Thresh:= Thresh*10.;
];
]</
{{out}}
|