# python - pyplot - title plt

## 按最小和生成整數的組合 (2)

``````# some primes for tesing
primes = [2]
x = 3
while x < 100000:
if all(x % p for p in primes):
primes.append(x)
x += 2

# main code

def find(tsum, tlen):

def _find(tsum, tlen, path, idx):
if tlen <= 0:
if tsum == 0:
yield path
return
while True:
p = primes[idx]
if p > tsum:
return
for f in _find(tsum - p, tlen - 1, path + [p], idx + 1):
yield f
idx += 1

return _find(tsum, tlen, [], 0)

for s in range(1001, 1002): # just for testing, should be range(28, max possible sum)
for comb in find(s, 5):
print s, comb``````

``````    (2, 3, 5, 7, 11)    sum = 28
(2, 3, 5, 7, 13)    sum = 30
(2, 3, 5, 7, 17)    sum = 34
(2, 3, 5, 11, 13)   sum = 34
(2, 3, 5, 7, 19)    sum = 36
(2, 3, 7, 11, 13)   sum = 36
(2, 3, 5, 11, 17)   sum = 38
(2, 5, 7, 11, 13)   sum = 38
(3, 5, 7, 11, 13)   sum = 39
(2, 3, 5, 7, 23)    sum = 40
(2, 3, 5, 11, 19)   sum = 40
(2, 3, 5, 13, 17)   sum = 40
(2, 3, 7, 11, 17)   sum = 40
(2, 3, 5, 13, 19)   sum = 42
(2, 3, 7, 11, 19)   sum = 42
(2, 3, 7, 13, 17)   sum = 42
(2, 5, 7, 11, 17)   sum = 42
...``````

• 有一個數組/素數列表，按升序排序。

• 保持索引五元組的優先級隊列，其中相應的素數之和作為鍵/權重/優先級/ whaddaycallit，初始填充權重`2+3+5+7+11 = 28`的五元組`(0,1,2,3,4)` `2+3+5+7+11 = 28`

• 雖然隊列不是空的，

1. `(a, b, c, d, e, f)` （當然是刪除它）。
2. 插入它的直接後繼者到隊列中，後繼者是那些

• `(a+1,b,c,d,e)`
• `(a,b+1,c,d,e)`
• `(a,b,c+1,d,e)`
• `(a,b,c,d+1,e)`
• `(a,b,c,d,e+1)`

嚴格上升，最後一個分量小於素數列表的長度

3. 產生以前得到的五倍