# python pyplot 按最小和生成整數的組合

## title plt (3)

``````    (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
...``````

``````#include <stdio.h>
#include <stdlib.h>
int v[100], stack[100];
int sp,i,j,k,n,g,s;
int main()
{
printf("Dimension of V:");
scanf( "%d",&n);
//Input numbers
for (i=0 ; i<n ; i++) {
printf("V[%d]=",i);
scanf( "%d",&g);
v[i]=g;
}
printf("subset number:");
scanf( "%d",&k);
printf("running...\n");
j=0;
s=0;
sp=-1;
while(1) {
// stack ones until
while(sp<n-1 && j<k && n-sp>k-j)  {
j=j+1;
sp=sp+1;
stack[sp]=1;
s=s+v[sp];
if(j==k) {
for (i=0 ; i<=sp ; i++) if (stack[i]==1) printf("%2d ",v[i]);
printf("sum=%d\n",s);
}
}
// unstack all the zeros until top of stack is equal one
while (stack[sp]==0 && sp>=0) sp=sp-1;
// if Bottom of stack is reached then stop
if (sp<0) break;
// set top of stack from one to zero
stack[sp]=0;
s=s-v[sp];
j=j-1;
}
return 0;
}``````

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

• 保持索引五元組的優先級隊列，其中相應的素數之和作為鍵/權重/優先級/ 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. 產生以前得到的五倍

``````# 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``````