Magic numbers: Difference between revisions

→‎{{header|jq}}: def extend:
(→‎{{header|jq}}: def extend:)
Line 175:
 
The solution presented here depends on the unbounded-precision integer
arithmetic of the Go implementations of jq:, and the results shown
can be generated by an invocation of the form:
 
<pre>
'''Invocation''': gojq -nr -f magic-numbers.jq
</pre>
<syntaxhighlight lang=jq>
def sum(s): reduce s as $x (0; .+$x);
 
# Emit all the polydivisibles in the form of an array of arrays
# such that the numbers in .[i] are the polydivisibles of length i+1
def polydivisible:
def extend($n):
{ numbers: [],
previous: [((. * 10) + range(10;10)],) | select(. % $n == 0);
# input: an array of arrays, such that the numbers in .[i] are the polydivisibles of length i+1
new: [],
def digitsextend: 2 }
| until ( .previous|length ==as 0;$in
| .numberslength +=as [.previous]$n
| reduce .previous[$in[-1][] as| extend($n+1)] (.;as $x
| if $x|length == reduce range(0;10) asthen $j (.;in
else $in + .number = 10 * [$nx] +| $jextend
| .new = []end;
| if (.number % .digits) == 0 then .new += [.number] else . end) )
[[range(1;10)]] | extend;
| .previous = .new
| .new = []
| .digits += 1 )
| .numbers;
 
def pandigital:
tostring | gsub("0";"") | explode | unique | length == 9;
 
# Select the pandigitals from .numbers[$k]
# Input: {numbers}an array as produced by polydivisible
# Output: an array
def pd($k):
.numbers[$k] | map(select(pandigital));
def tasks:
{numbers: polydivisible}
| .numbers[0] += [0]
| "There are \(sum(.numbers[] | length)) magic numbers in total.",
"\nThe largest is \(.numbers[-1][-1])",
"\nThere are:",
(range(0; .numbers|length) as $i
| "\(.numbers[$i]|length) with \($i + 1) digit\(if ($i == 0) then "" else "s" end)"),
( "\nAll magic numbers that are pan-digital in 1 through 9 with no repeats: ", pd(8)[] ),
( "\nAll magic numbers that are pan-digital in 0 through 9 with no repeats: ", pd(9)[] ) ;
Line 258 ⟶ 259:
3816547290
</pre>
 
 
=={{header|Phix}}==
2,449

edits