我已經建立一個程序,發現在這個等式中8個不同的常數:使用蠻力(A-H < = 5)8嵌套循環可能組合
a*40 +b*0 +c*3 +d*10 +e*10 +f*0 +g*9 +h*7.5 =292(+-5)
a*4 +b*7 +c*5 +d*3 +e*0 +f*0 +g*7 +h*0 =63(+-5)
a*0 +b*6 +c*3 +d*0 +e*0 +f*5 +g*7 +h*0 =85(+-5)
a*175 +b*50 +c*50 +d*75 +e*75 +f*50 +g*110 +h*50 =635(+-5)
。 但它需要很長時間(我知道,我知道你不需要說) 我怎麼能加快這個過程?
基本上這是我的代碼。在現實中,我的程序有4個:
chofound=[]
konstanta=[5,5,5,5,5,5,5,5]
## konstanta=[10,0,5,8,2,0,4,
for h in range(0,5):
for g in range(0,5):
for f in range(0,5):
for e in range(0,5):
for d in range(0,5):
for c in range(0,5):
for b in range(0,5):
for a in range(0,5):
hasil= a*konstanta[0]+\
b*konstanta[1]+\
c*konstanta[2]+\
d*konstanta[3]+\
e*konstanta[4]+\
f*konstanta[5]+\
g*konstanta[6]+\
h*konstanta[7]
if (hasil>=(292-5) and hasil <=(292+5)):
asd=[a,b,c,d,e,f,g,h]
print ("found with config: {}".format(asd))
chofound.append(asd)
return chofound
有沒有任何有效的方法來真正知道沒有暴力的a-h?或者使我的代碼有效運行的任何算法?
我有什麼你正在嘗試做的,但猜'itertools'的關鍵是這樣做沒有真正的想法不太詳細。對於現代計算機來說,5 ** 8次迭代也是微不足道的。無論你在做什麼,循環本身不是瓶頸。也許這就是所有的印刷。 –
使用來自'numpy'的'array'類將允許您將其重寫爲矢量操作,並且應該比for循環快得多。 – Craig
292(+ - 5)是否意味着您的解決方案具有+/- 5的容差? – Crispin