Python脚本打印n个素数集

时间:2019-02-22 05:57:37

标签: python

我是编程新手。请帮助我更正我的代码。

n = int(input("Enter the number: "))
s = set()
while True:
    for i in range(2, n):
        if n%i==0:
            break
        else:
            s.add(i)
print(s)

3 个答案:

答案 0 :(得分:0)

第一和基本筛分算法

T = int(input("Limit").rstrip())
def genp(n):                      # General algo.
    for i in range(2, n):
        var1 = True
        for j in range(2, i):
            if i%j==0:
                var1 = False
                break
        if var1:
            yield i
# if T = 300
>>> list(genp(T))  
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]

使用平方根策略的第二个改进算法(比第一个更快)

def genp2(n):                              
    upper = int(n**0.5) + 1
    if upper <= 2:
        yield 2
        return
    for i in range(upper ,n):
        var1 = True
        for j in range(2, upper):
            if j <= i:
                if i%j==0:
                    var1 = False
                    break
        if var1:
            yield i
    t = genp2(upper)
    for i in t: yield i

>>> sorted(list(genp2(T)))  # sorting the unorganized list
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]

在质数直到第n位数字之前的最终和最佳算法(其中) 更有效地使用平方根方法 (最快)

def root(n):
    Root = []
    upper = int(n**0.5) + 1
    while int(upper)> 2:
        Root = [upper] + Root
        upper = int(upper**0.5) + 1
    return Root

Root = root(T)

def genp3(n):
    for i in range(2, n):
        var1 = True
        for j in primes:
            if j>= i: continue
            if j<i:
                if i%j==0:
                    var1 = 0
                    break
        if var1: yield i

primes = [2, 3]

for i in Root+[T]:
    primes += list(genp3(i))

primes = list(set(primes))

>>> print(sorted(primes))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]

按时间进行比较:

from timeit import timeit

s1 = '''
def genp1(n):
    for i in range(2,n):
        var1 = 1
        for j in range(2, i):
            if i%j==0:
                var1 = 0
                break
        if var1:
            yield i
l1 = list(genp1({}))

'''.format(T)

s2 = '''
def genp2(n):                           
    upper = int(n**0.5) + 1
    if upper <= 2:
        yield 2
        return
    for i in range(upper ,n):
        var1 = True
        for j in range(2, upper):
            if j <= i:
                if i%j==0:
                    var1 = False
                    break
        if var1:
            yield i
    t = genp2(upper)
    for i in t: yield i
l2 = sorted(list(genp2({})))
'''.format(T)                        # Better algo


s3 = ''' 
def root(n):
    Root = []
    upper = int(n**0.5) + 1
    while int(upper)> 2:
        Root = [upper] + Root
        upper = int(upper**0.5) + 1
    return Root
                                                    # Root
Root = root({})

def genp3(n):
    for i in range(2, n):
        var1 = True
        for j in primes:
            if j>= i: continue
            if j<i:
                if i%j==0:
                    var1 = 0
                    break
        if var1: yield i

primes = [2, 3]

for i in Root+[{}]:
    primes += list(genp3(i))

#print(primes)
'''.format(T, T)

# for T = 200,000;
>>> print(timeit(stmt = s1, number = 1))
121.92417089999799
>>> print(timeit(stmt = s2, number = 1))
0.8547031289999723
>>> print(timeit(stmt = s3, number = 1))
0.28995667900016997

# for T = 104730; (measuring speed for 10,000 primes; 10,000th prime is 104729)
>>> print(timeit(stmt =  s1, number = 1))
35.426819173000695                            #Good
>>> print(timeit(stmt =  s2, number = 1))
0.35784729400256765                           #Better
>>> print(timeit(stmt =  s3, number = 1))
0.13226230800137273                           #Best

答案 1 :(得分:0)

尝试一下:

n = int(input("Enter the number: "))
s = set()

for i in range(2, n+1):
    flag = 1
    for j in range(2, i):    
        if i%j == 0:
            flag = 0
            break
    if flag == 1:        
        s.add(i)

print(s)

答案 2 :(得分:0)

这里有两个问题。 while True循环永无止境; break语句只是中断内部循环,因此它将永远运行。您也不会迭代最多n个数字,因此实际上您只是在for循环中检查n是否为质数。

请尝试以下操作:

n = int(input("Enter the number: "))
primes = set()
for i in range(2, n): # Check each number i up to n
    for j in range (2, n): # Check if i is prime
        if j>i/2: # if j>i/2, i is prime
            primes.add(i)
            break
        if i%j==0:
            break # Not prime
print(primes)

请注意,python中的set没有(也不能被排序)。如果要按顺序填入素数,请尝试使用list