Finding the nth number of primes

Posted by Braxton Smith on Stack Overflow See other posts from Stack Overflow or by Braxton Smith
Published on 2010-06-12T21:49:36Z Indexed on 2010/06/12 22:02 UTC
Read the original article Hit count: 352

Filed under:
|
|

I can not figure out why this won't work. Please help me

from math import sqrt

pN = 0 
numPrimes = 0
num = 1

def checkPrime(x):
   '''Check\'s whether a number is a prime or not'''
   prime = True
   if(x==2):
      prime = True
   elif(x%2==0):
      prime=False
   else:
      root=int(sqrt(x))
      for i in range(3,root,2):
         if(x%i==0):
            prime=False
            break
   return prime

n = int(input("Find n number of primes. N being:"))

while( numPrimes != n ):
   if(  checkPrime( num ) == True ):
      numPrimes += 1
      pN = num
      print("{0}: {1}".format(numPrimes,pN))
   num += 1

print("Prime {0} is: {1}".format(n,pN))

© Stack Overflow or respective owner

Related posts about math

Related posts about computer-science