Numbers with prime digits whose sum is 13: Difference between revisions

(Added 11l)
Line 1,013:
52222,222223,222232,222322,223222,232222
322222</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
Two solutions are presented. Both are based on the
observation that the only digits which are prime are [2, 3, 5, 7]
so the numbers of interest cannot have more than 6 digits.
To save space, both also present the count of the number of solutions
using the stream counter:
 
def count(s): reduce s as $_ (0; .+1);
 
'''Simple Generate-and-Test Solution'''
<lang jq># Output: a stream
def simple:
range(2; 7) as $n
| [2, 3, 5, 7]
| combinations($n)
| select(add == 13)
| join("") | tonumber;
 
count(simple)</lang>
''A Faster Solution'''
<lang jq>def faster:
def digits: [2, 3, 5, 7];
def wide($max):
def d: digits[] | select(. <= $max);
if . == 1 then d | [.]
else d as $first
| (. - 1 | wide($max - $first)) as $next
| [$first] + $next
end;
range(2; 7)
| wide(13)
| select(add == 13)
| join("") | tonumber;
 
count(faster)
</lang>
{{out}}
In both cases:
<pre>
43
</pre>
 
=={{header|Julia}}==
2,461

edits