To find first N prime numbers in python

Posted by Rahul Tripathi on Stack Overflow See other posts from Stack Overflow or by Rahul Tripathi
Published on 2009-10-27T05:44:35Z Indexed on 2010/06/15 5:42 UTC
Read the original article Hit count: 388

Filed under:
|

Hi All,

I am new to the programming world. I was just writing this code in python to generate N prime numbers. User should input the value for N which is the total number of prime numbers to print out. I have written this code but it doesn't throw the desired output. Instead it prints the prime numbers till the Nth number. For eg.: User enters the value of N = 7. Desired output: 2, 3, 5, 7, 11, 13, 19 Actual output: 2, 3, 5, 7

Kindly advise.

i=1
x = int(input("Enter the number:"))
for k in range (1, (x+1), 1):
    c=0
    for j in range (1, (i+1), 1):
        a = i%j
        if (a==0):
            c = c+1

    if (c==2):
          print (i)
    else:
          k = k-1

    i=i+1

© Stack Overflow or respective owner

Related posts about python

Related posts about primes