Magic numbers: Difference between revisions

m (C++ solution uses Boost library)
Line 168:
3816547290
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with gojq, the Go implementation of jq, and with fq'''
 
The solution presented here depends on the unbounded-precision integer
arithmetic of the Go implementations of jq:
 
'''Invocation''': gojq -nr -f magic-numbers.jq
<syntaxhighlight lang=jq>
def sum(s): reduce s as $x (0; .+$x);
 
def polydivisible:
{ numbers: [],
previous: [range(1;10)],
new: [],
digits: 2 }
| until (.previous|length == 0;
.numbers += [.previous]
| reduce .previous[] as $n (.;
reduce range(0;10) as $j (.;
.number = 10 * $n + $j
| if (.number % .digits) == 0 then .new += [.number] else . end) )
| .previous = .new
| .new = []
| .digits += 1 )
| .numbers;
 
def pandigital:
tostring | gsub("0";"") | explode | unique | length == 9;
 
# Select the pandigitals from .numbers[$k]
# Input: {numbers} 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)[] ) ;
 
tasks
</syntaxhighlight>
{{output}}
<pre>
There are 20457 magic numbers in total.
 
The largest is 3608528850368400786036725
 
There are:
10 with 1 digit
45 with 2 digits
150 with 3 digits
375 with 4 digits
750 with 5 digits
1200 with 6 digits
1713 with 7 digits
2227 with 8 digits
2492 with 9 digits
2492 with 10 digits
2225 with 11 digits
2041 with 12 digits
1575 with 13 digits
1132 with 14 digits
770 with 15 digits
571 with 16 digits
335 with 17 digits
180 with 18 digits
90 with 19 digits
44 with 20 digits
18 with 21 digits
12 with 22 digits
6 with 23 digits
3 with 24 digits
1 with 25 digits
 
All magic numbers that are pan-digital in 1 through 9 with no repeats:
381654729
 
All magic numbers that are pan-digital in 0 through 9 with no repeats:
3816547290
</pre>
 
 
=={{header|Phix}}==
2,442

edits