Wednesday, 4 December 2013

Project Euler Problem 7 - Solved

Q) 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?

Solution: Python:
=============== from math import sqrt db= [1,2,3] def Prime(x): num = 5 while True: num1 = sqrt(num) try: ans = db[x] break except IndexError: for i in db[2:]: if not(num%i): break elif i>num1: break if (num%i): db.append(num) num += 2 return ans print Prime(10001)

No comments:

Post a Comment