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
 
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),
data = [(99809, 1), (18, 2), (19, 3), (20, 4), (2017, 24), (22699, 1), (22699, 2), (22699, 3), (22699, 4), (40355, 3)]
 
 
for n, cnt in data:
ci = iter(cmb(genP(n), cnt))
while True:
try:
c = next(ci)
if sum(c) == n:
print(n, ',', cnt , "->", '+'.join(str(s) for s in c))
[repr((n, cnt)), "->", '+'.join(str(s) for s in c)]
))
break
except StopIteration:
print(repr((n, ',',cnt)) cnt,+ " -> Not possible")
break</lang>
</lang>
{{out}}
<pre>(99809, 1) -> 99809
99809 (18, 12) -> 998095+13
18 (19, 23) -> 3+5+1311
19 (20, 34) -> 3+5+11Not possible
(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
20 , 4 -> Not possible
(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 , 12) -> 226992+22697
(22699 , 23) -> 23+5+2269722691
(22699 , 34) -> 2+3+543+2269122651
22699 (40355, 43) -> 2+3+43139+2265140213</pre>
40355 , 3 -> 3+139+40213
</pre>
 
=={{header|Racket}}==
9,655

edits