Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Magic-constant/Jq/magic-constant-1.jq
Normal file
36
Task/Magic-constant/Jq/magic-constant-1.jq
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# 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;
|
||||
14
Task/Magic-constant/Jq/magic-constant-2.jq
Normal file
14
Task/Magic-constant/Jq/magic-constant-2.jq
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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))" )
|
||||
Loading…
Add table
Add a link
Reference in a new issue