top of page

Finding the 10,001st Prime Number - Project Euler Problem 7

2 days ago

1 min read

0

0

0

Problem 7 asks: What is the 10,001st prime number?

My approach uses a straightforward primality test with trial division up to the square root of each candidate number. Here's the complete Python solution:

python

import math

def is_prime(n):
    if n < 2:
        return False
    
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    
    return True

prime_list = []
for i in range(0, 999999):
    if is_prime(i):
        prime_list.append(i)

print(prime_list[10000])

The solution builds a list of all primes up to 999,999, then retrieves the prime at index 10,000 (which is the 10,001st prime due to zero-indexing).

Answer: 104,743

While this works, there's room for optimization - the Sieve of Eratosthenes would be more efficient for generating multiple primes, and we could stop once we've found exactly 10,001 primes rather than checking numbers up to an arbitrary upper bound.

#ProjectEuler #Python #PrimeNumbers #Algorithms

2 days ago

1 min read

0

0

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page