Partition an integer x into n primes: Difference between revisions
m
→{{header|Python}}: Applied some of the pylint and autopep recommendations. Updated output.
m (→{{header|Haskell}}: Used Data.Numbers.Primes, tidied.) |
m (→{{header|Python}}: Applied some of the pylint and autopep recommendations. Updated output.) |
||
Line 1,298:
=={{header|Python}}==
<lang Python>from itertools import combinations as cmb
def isP(n):
if n == 2:
return True
if n % 2 == 0:
return False
return all(n % x > 0 for x in range(3, int(n ** 0.5) + 1, 2))
def genP(n):
p = [2]
p.extend([x for x in range(3, n + 1, 2) if isP(x)])
return p
data = [(99809, 1), (18, 2), (19, 3), (20, 4), (2017, 24), (22699, 1), (22699, 2), (22699, 3), (22699, 4), (40355, 3)]▼
data = [
(99809, 1), (18, 2), (19, 3), (20, 4), (2017, 24),
▲
for n, cnt in data:
ci = iter(cmb(genP(n), cnt))
while True:
try:
c = next(ci)
if sum(c) == n:
print(
[repr((n, cnt)), "->", '+'.join(str(s) for s in c)]
))
break
except StopIteration:
print(repr((n,
break</lang>
{{out}}
<pre>(99809, 1) -> 99809
(22699, 1) -> 22699
▲2017 , 24 -> 2+3+5+7+11+13+17+19+23+29+31+37+41+43+47+53+59+61+67+71+73+79+97+1129
(22699
(22699
(22699
=={{header|Racket}}==
|