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

Content added Content deleted
(already break when equal)
(→‎Python: rewrite as a generator)
Line 1,932: Line 1,932:
=={{header|Python}}==
=={{header|Python}}==
With improvements to the ideas from the discussion page:
With improvements to the ideas from the discussion page:
<syntaxhighlight lang="python">src, dst = [(13, 0)], []
<syntaxhighlight lang="python">from collections import deque

while src:
def prime_digits_sum(r):
r, n = src.pop(0)
for d in 2, 3, 5, 7:
q = deque([(r, 0)])
if d >= r:
while q:
if d == r:
r, n = q.popleft()
dst.append(n * 10 + d)
for d in 2, 3, 5, 7:
break
if d >= r:
src.append((r - d, n * 10 + d))
if d == r: yield n * 10 + d
break
print(*dst)</syntaxhighlight>
q.append((r - d, n * 10 + d))

print(*prime_digits_sum(13))</syntaxhighlight>
{{out}}
{{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>
<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>