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

→‎{{header|Factor}}: add a solution based on Nigel Galloway's algorithm as described on the talk page
(→‎{{header|JavaScript}}: As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page.)
(→‎{{header|Factor}}: add a solution based on Nigel Galloway's algorithm as described on the talk page)
Line 82:
</pre>
=={{header|Factor}}==
===Filtering selections===
Generate all selections of the prime digits in the only possible lengths whose sum can be 13, then filter for sums that equal 13.
<lang factor>USING: formatting io kernel math math.combinatorics
math.functions math.ranges sequences sequences.extras ;
Line 94 ⟶ 96:
Numbers whose digits are prime and sum to 13:
{ 337, 355, 373, 535, 553, 733, 2227, 2272, 2335, 2353, 2533, 2722, 3235, 3253, 3325, 3352, 3523, 3532, 5233, 5323, 5332, 7222, 22225, 22252, 22333, 22522, 23233, 23323, 23332, 25222, 32233, 32323, 32332, 33223, 33232, 33322, 52222, 222223, 222232, 222322, 223222, 232222, 322222 }
</pre>
===F# translation===
The following is based on Nigel Galloway's algorithm as described [http://rosettacode.org/wiki/Talk:Numbers_with_prime_digits_whose_sum_is_13#Nice_recursive_solution here] on the talk page. It's about 10x faster than the previous method.
<lang factor>USING: io kernel math prettyprint sequences sequences.extras ;
 
{ } { { 2 } { 3 } { 5 } { 7 } } [
{ 2 3 5 7 } [ suffix ] cartesian-map concat
[ sum 13 = ] partition [ append ] dip [ sum 11 > ] reject
] until-empty [ bl ] [ [ pprint ] each ] interleave nl</lang>
{{out}}
<pre>
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
</pre>
 
1,820

edits