Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
import math prime=True for num in range(1,1000): x=math.sqrt(num) x=int(x) for i in range(2,x): if(num%i==0): prime=False else: print(num)
我试图找到素数,但我似乎无法理解为什么这不起作用。我试图使用平方根来找到问题
您需要在打印前测试所有数字。
for i in range(2,x): if(num%i==0): prime=False break # optimisation - see comments if prime: print(num)
此外,将每个数字的素数重置为 True:
for num in range(1,1000): prime = True x=math.sqrt(num)