aboutsummaryrefslogtreecommitdiff
path: root/challenge-172/eric-cheung/python/ch-1.py
blob: 7ef1bf9218ef50cb38861f6f104a8d371cf23a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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