Numbers with prime digits whose sum is 13: Difference between revisions
Content added Content deleted
(→{{header|Factor}}: add a solution based on Nigel Galloway's algorithm as described on the talk page) |
(→{{header|Phix}}: iterative version) |
||
Line 541: | Line 541: | ||
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222 |
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222 |
||
</pre> |
</pre> |
||
===iterative=== |
|||
Queue-based version of Nigel's recursive algorithm, same output. |
|||
<lang Phix>requires("0.8.2") -- uses latest apply() mods, rest is fine |
|||
constant dgts = {2,3,5,7} |
|||
function unlucky() |
|||
sequence res = {}, q = {{0,0}} |
|||
integer s, -- partial digit sum, <=11 |
|||
v -- corresponding value |
|||
while length(q) do |
|||
{{s,v}, q} = {q[1], q[2..$]} |
|||
for i=1 to length(dgts) do |
|||
integer d = dgts[i], {ns,nv} = {s+d,v*10+d} |
|||
if ns<=11 then q &= {{ns,nv}} |
|||
elsif ns=13 then res &= nv end if |
|||
end for |
|||
end while |
|||
return res |
|||
end function |
|||
sequence r = unlucky() |
|||
r = apply(true,sprintf,{{"%6d"},r}) |
|||
puts(1,join_by(r,1,11," "))</lang> |
|||
I've archived a slightly more OTT version: [[Numbers_with_prime_digits_whose_sum_is_13/Phix]]. |
I've archived a slightly more OTT version: [[Numbers_with_prime_digits_whose_sum_is_13/Phix]]. |
||