以下函数为我提供了正确的结果。但是对于大量的q,这个程序就很难处理了。所以,我想通过循环或其他方式迭代 q。我该怎么做?
def sgf(a): # here a is a list of two numbers
import random
a2=random.randint(1,1068)
p=1069
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
while ((q1>1060) or (q2>1060) or (q3>1060) or (q4>1060) or (q5>1060) or (q6>1060) or (q7>1060) or (q8>1060) or (q9>1060)):
a2=random.randint(1,1068)
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
if ((q1<=1060) and (q2<=1060) and (q3<=1060) and (q4<=1060) and (q5<=1060) and (q6<=1060) and (q7<=1060) and (q8<=1060) and (q9<=1060)):
break
return q1,q2,q3,q4,q5,q6,q7,q8,q9
为简单起见,
f(x)=(a0+a1*x+a2*x**2)%p
其中 a0,a1 在 [0,1060] 中,a2 从 [0,1068] 中随机选择
如果所有 f(x)<=1060
, 接受 f(x) 否则重新生成 f(x)
最佳答案
您的 q
应该是一个列表。这样你就可以使用 for
循环和 range
来执行你的计算:
for ind in range(len(q)):
q[ind]=(a[0]+a[1]*(ind + 1)+a2*(ind + 1)**2)%p
您还可以将您的条件表示为迭代,但最简单的方法可能是使用 any
函数并将您的条件编写为生成器:
while any(qx > 1060 for qx in q):
关于python - 如何在 python 中迭代条件检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245559/