Magic constant: Difference between revisions

m
syntax highlighting fixup automation
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.
<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:
print( ( "10^", whole( n, -2 ), ": ", whole( inv a( e ), -9 ), newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 99:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">a: function [n][
n: n+2
return (n*(1 + n^2))/2
Line 119:
print ""
loop 1..19 'z ->
print ["10 ^" z "=>" aInv 10^z]</langsyntaxhighlight>
 
{{out}}
Line 150:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MAGIC_CONSTANT.AWK
# converted from FreeBASIC
Line 175:
return(k)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 204:
=={{header|Basic}}==
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="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</langsyntaxhighlight>
{{out}}<pre>
The first 20 magic constants are
Line 256:
 
==={{header|QB64}}===
<langsyntaxhighlight lang="qbasic">$NOPREFIX
 
DIM order AS INTEGER
Line 281:
FUNCTION MagicSum&& (n AS INTEGER)
MagicSum&& = (n * n + 1) / 2 * n
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre>
Line 309:
 
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">function a(n)
n = n + 2
return n*(n^2 + 1)/2
Line 332:
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:
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:
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:
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:
print "10^", e using"##", ": ", inv_a(10^e) using "#########"
next e
end</langsyntaxhighlight>
 
 
=={{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:
over integer-log10 over "10^%02d: %d\n" printf
dup + 1 -
] times 2drop</langsyntaxhighlight>
{{out}}
<pre>
Line 494:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 538:
fmt.Printf("10%-2s : %9s\n", superscript(i), rcu.Commatize(order))
}
}</langsyntaxhighlight>
 
{{out}}
Line 547:
=={{header|J}}==
 
Implementation:<langsyntaxhighlight Jlang="j">mgc=: 0 0.5 0 0.5&p.</langsyntaxhighlight>
 
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:<langsyntaxhighlight Jlang="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
Line 565:
8 585 100101105
9 1260 1000188630
10 2715 10006439295</langsyntaxhighlight>
stretch example:<langsyntaxhighlight Jlang="j"> ((10+#\),.],.mgc) x:(mgc i.6e6) I.10^11+i.10
11 5849 100049490449
12 12600 1000188006300
Line 576:
18 1259922 1000002262299152685
19 2714418 10000004237431278525
20 5848036 100000026858987459346</langsyntaxhighlight>
 
=={{header|jq}}==
Line 584:
 
'''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 619:
elif . < 20 then ss[1] + ss[. - 10]
else ss[2] + ss[0]
end;</langsyntaxhighlight>
 
'''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))" )</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="julia">using Lazy
 
magic(x) = (1 + x^2) * x ÷ 2
Line 655:
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 683:
 
=={{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 693:
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 750:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Magic_constant
Line 765:
{
printf "%3d %9d\n", $i, (10 ** $i * 2) ** ( 1 / 3 ) + 1;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 797:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 846:
=={{header|Prolog}}==
Minimalistic, efficient approach
<langsyntaxhighlight lang="prolog">
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>
</lang>
{{out}}
<pre>
Line 888:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def a(n):
Line 908:
for e in range(1, 20):
print(f'10^{e}: {inv_a(10**e)}');</langsyntaxhighlight>
{{out}}
<pre>The first 20 magic constants are:
Line 935:
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery"> [ 3 + dup 3 ** + 2 / ] is magicconstant ( n --> n )
20 times [ i^ magicconstant echo sp ] cr cr
Line 948:
[ 10 21 ** ] constant
over = until ]
2drop</langsyntaxhighlight>
 
{{out}}
Line 980:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers:ver<2.8+>;
 
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 ) }</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 1,016:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func f(n) {
(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))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,060:
{{libheader|Wren-fmt}}
This uses Julia's approach for the final parts.
<langsyntaxhighlight lang="ecmascript">import "./seq" for Lst
import "./fmt" for Fmt
Line 1,081:
var order = (goal * 2).cbrt.floor + 1
Fmt.print("10$-2s : $,9d", superscript.call(i), order)
}</langsyntaxhighlight>
 
{{out}}
Line 1,119:
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,145:
Thresh:= Thresh*10.;
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits