我的python prime finder中的无限范围?

4 python primes

我想在我的python素数查找器中获得无限范围!这是我的代码!

import math
print "Welcome to Prime Finder!"
option = raw_input("continue(y/n)")
 if option == "y":
    for num in range(1,(infinite number)):
        if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
           print num
Run Code Online (Sandbox Code Playgroud)

我试图得到它所说的(无限数)实际上是一个无限数.有什么价值或东西我可以用来找到它吗?任何帮助将不胜感激!

Jan*_*cak 10

您可以导入itertools并使用count函数

import itertools
for num in itertools.count(1):
    print num
Run Code Online (Sandbox Code Playgroud)

count(1) - > 1 2 3 4 5 ...

数(10) - > 10 11 12 13 14 ...

数(1,2) - > 1 3 5 7 9 ...

第一个参数是起点.


sam*_*rap 5

你可以使用while循环

num = 1
while True:
    option = raw_input("continue(y/n)")
    if option != "y": break

    if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
       print num
    num += 1
Run Code Online (Sandbox Code Playgroud)