Kibbee.ca
icon
Project Euler: Problem 7

What is the 10001st prime number?

[Full Question]

from math import sqrt

#this function will determine if 
# num is prime
def isPrime(num):
    i = 2
    isPr = True
    numSqrt = sqrt(num)
    while i <= numSqrt and isPr:
        if num % i == 0:
            isPr = False

        i += 1
    return isPr

primesFound = 1
i = 3

while primesFound < 10001:
    if isPrime(i):
        primesFound += 1
        lastPrime = i
    i += 2
    
print "The 10001st Prime Is %d" % (lastPrime,) 

[Download Code]

Another pretty straightforward question. I used the same function as before to figure out if hte number is prime. I'm starting at 3, and starting the count at 1, so that I don't have to check all the even numbers. There's probably a better way of checking if a number is prime, but this way seems to be pretty efficient for most of the problems I've seen so far.

No Comments Posted