// you’re reading...

Project Euler Solutions

Project Euler Problem 7 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Problem Description

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th prime is 13.

What is the 10001st prime number?

Analysis

Having a sufficiently fast is_prime() funtion makes solving this problem quick and easy. Just enumerate the positive odd integers from 7 that follow the sequence 6m±1 until the nth prime is found. For this problem n = 10001.

Solution

Runs < 1 second in Python.

from Euler import is_prime  
 
n = 10001
p = 5; f = 1
while n>3:   # prime numbers  2, 3 and 5 are accounted for
  p += 3-f
  f = -f
  if is_prime(p): n -= 1
print "Answer to PE7 = ", p

Comments

Click here for more information on the is_prime() function.
It took 34,913 iterations to solve this problem. Checking just odd numbers from 3 by 2′s takes 52,371 iterations.

Discussion

No comments for “Project Euler Problem 7 Solution”

Post a comment