简体   繁体   中英

Python script to print set of n prime numbers

I am new to programming. Please help me in correcting my code.

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)

First and Basic sieve algo

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]

Second and improved algorithm with sq. root strategy use(Faster than first)

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]

Final and best algorithm(among these) for primes till nth digit Uses sq. root method in more efficient way (Fastest)

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]

Comparing all by timeit:

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

Try this:

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)

There's a couple of issues here. The while True loop never ends; the break statement just breaks out of the inner loop, so this will run forever. You never iterate through the numbers up to n either, so you are really just checking if n is prime with the for loop.

Try the following:

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)

Note that set in python is not (and cannot be) ordered. If you want the primes in order, try using a list .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM