aboutsummaryrefslogtreecommitdiff
path: root/challenge-172/eric-cheung/python/ch-1.py
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-05 09:19:32 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-05 09:19:32 +0100
commit3050196e253fa050142303497686d9eb40a5cc04 (patch)
tree7cc4201e189c28a0e1375d3a0c843e4569a03500 /challenge-172/eric-cheung/python/ch-1.py
parenta5191b0b44da36ea80dcbd71694ee8582bd2c579 (diff)
downloadperlweeklychallenge-club-3050196e253fa050142303497686d9eb40a5cc04.tar.gz
perlweeklychallenge-club-3050196e253fa050142303497686d9eb40a5cc04.tar.bz2
perlweeklychallenge-club-3050196e253fa050142303497686d9eb40a5cc04.zip
- Added guest contributions by Eric Cheung.
Diffstat (limited to 'challenge-172/eric-cheung/python/ch-1.py')
-rwxr-xr-xchallenge-172/eric-cheung/python/ch-1.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-172/eric-cheung/python/ch-1.py b/challenge-172/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..7ef1bf9218
--- /dev/null
+++ b/challenge-172/eric-cheung/python/ch-1.py
@@ -0,0 +1,46 @@
+
+## Remarks
+## https://rosettacode.org/wiki/Partition_an_integer_x_into_n_primes#Python
+
+from itertools import combinations as cmb
+
+def IsPrime(nInput):
+
+ if nInput == 2:
+ return True
+
+ if nInput % 2 == 0:
+ return False
+
+ return all(nInput % xLoop > 0 for xLoop in range(3, int(nInput ** 0.5) + 1, 2))
+
+
+def genP(nInput):
+
+ pArr = [2]
+ pArr.extend([xLoop for xLoop in range(3, nInput + 1, 2) if IsPrime(xLoop)])
+
+ return pArr
+
+
+## dataArr = [(99809, 1), (18, 2), (19, 3), (20, 4), (2017, 24), (22699, 1), (22699, 2), (22699, 3), (22699, 4), (40355, 3)]
+dataArr = [(18, 2), (19, 3)]
+
+
+for nNum_01, nNum_02 in data:
+
+ nIter = iter(cmb(genP(nNum_01), nNum_02))
+
+ while True:
+ try:
+ nNextSum = next(nIter)
+ if sum(nNextSum) == nNum_01:
+
+ print (" ".join([repr((nNum_01, nNum_02)), "->", " + ".join(str(nSumLoop) for nSumLoop in nNextSum)]))
+ break
+
+ except StopIteration:
+
+ print (repr((nNum_01, nNum_02)) + " -> Not possible")
+ break
+