Project Euler 7: (Iron)Python

Posted by Ben Griswold on Johnny Coder See other posts from Johnny Coder or by Ben Griswold
Published on Mon, 13 Sep 2010 02:46:46 +0000 Indexed on 2010/12/06 16:59 UTC
Read the original article Hit count: 323

In my attempt to learn (Iron)Python out in the open, here’s my solution for Project Euler Problem 7

As always, any feedback is welcome.

# Euler 7
# http://projecteuler.net/index.php?section=problems&id=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 10001st prime number?
import time
start = time.time()

def nthPrime(nth):
    primes = [2]
    number = 3          

    while len(primes) < nth:
        isPrime = True
        for prime in primes:
            if number % prime == 0:
                isPrime = False
                break
            if (prime * prime > number):
                break
        if isPrime:
            primes.append(number)
        number += 2                 

    return primes[nth - 1]

print nthPrime(10001)
print "Elapsed Time:", (time.time() - start) * 1000, "millisecs"
a=raw_input('Press return to continue')

© Johnny Coder or respective owner

Related posts about ironpython

Related posts about languages