Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: 10001st prime : Problem 7 Euler Project

Thursday, August 28, 2014

10001st prime : Problem 7 Euler Project

10001st prime:Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?

Python Code

import math
def countPrime():
    primeCount = 0
    prime = 1
    counter = 2
    def checkPrime(numb):
        prime = True
        for i in range(int(math.sqrt(numb))):
            i = i+2
            if numb%i == 0:
                prime = False
                break
        if numb == 2:
            return True
        else:           
            return prime
    while primeCount < 10001:
         if checkPrime(counter) == True:
             primeCount = primeCount+1
             prime = counter
         counter = counter + 1   
    print "10001 st Prime is",prime        
countPrime()

No comments:

Post a Comment