Magic constant: Difference between revisions

Line 245:
10^20: 5848036
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''<br>'''Works with jq''' (*)<br>'''Works with gojq, the Go implementation of jq'''
 
(*) The arithmetic precision of the C implementation of jq is insufficient for the extended task.
 
'''Preliminaries'''
<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);
 
# nth-root
def iroot($n):
. as $in
| if $n == 1 then .
else (. < 0) as $neg
| if $neg and (n % 2) == 0
then "Cannot take the \($n)th root of a negative number." | error
else ($n-1) as $n
| {t: (if $neg then -. else . end)}
| .s = .t + 1
| .u = .t
| until (.u >= .s;
.s = .u
| .u = ((.u * $n) + (.t / (.u|power($n)))) / ($n + 1) )
| if $neg then - .s else .s end
end
end;
 
# input: an array
# output: a stream of arrays of size size except possibly for the last array
def group(size):
recurse( .[size:]; length>0) | .[0:size];
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def ss : ["\u2070", "\u00b9", "\u00b2", "\u00b3", "\u2074",
"\u2075", "\u2076", "\u2077", "\u2078", "\u2079"];
def superscript:
if . < 10 then ss[.]
elif . < 20 then ss[1] + ss[. - 10]
else ss[2] + ss[0]
end;</lang>
 
'''The Task'''
<lang>
def magicConstant: (.*. + 1) * . / 2;
"First 20 magic constants:",
([range(3;23)
| magicConstant]
| group(10) | map(lpad(5)) | join(" ")),
"",
"1,000th magic constant: \( 1002| magicConstant)",
"",
"Smallest order magic square with a constant greater than:",
(range(1; 21) as $i
| (10 | power($i)) as $goal
| ((($goal * 2)|iroot(3) + 1) | floor) as $order
| ("10\($i|superscript)" | lpad(5)) + ": \($order|lpad(9))" )</lang>
{{out}}
As for [[#Wren|Wren]] except for commatization.
 
 
=={{header|Julia}}==
Line 287 ⟶ 350:
10^20 5848036
</pre>
 
 
=={{header|Perl}}==
2,442

edits