Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #7 easy

10001st Prime

By listing the first six prime numbers: 2,3,5,7,112, 3, 5, 7, 11, and 1313, we can see that the 66th prime is 1313.

What is the 1000110\,001st prime number?

View on Project Euler

Implementations

cpp
// https://projecteuler.net/problem=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?
// Answer: 104743
#include <iostream>
#include <memory>
#include "sieve_eratos.h"
int nth_prime(size_t nth)
{
std::unique_ptr<CSieveOfEratosthenes> sieve(new CSieveOfEratosthenes(110000));
if( sieve ){
return sieve->get_nth(nth);
}
return 0;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[])
{
std::cout << "Answer: " << nth_prime(10001) << std::endl;
}
#endif
View on GitHub
O(n) time complexity

Solution Notes

Mathematical Background

Prime numbers are integers greater than 1 that have no positive divisors other than 1 and themselves. The nth prime number refers to the prime at position n in the sequence of primes ordered by size.

The prime number theorem provides an asymptotic approximation: the nth prime is approximately nlnnn \ln n. For n=10,001, this gives roughly 10,001 × ln(10,001) ≈ 10,001 × 9.21 ≈ 92,000, which is close to the actual value of 104,743.

The distribution of primes becomes sparser as numbers grow, following the prime number theorem: π(n)nlnn\pi(n) \approx \frac{n}{\ln n} where π(n)\pi(n) is the number of primes ≤ n.

Algorithm Analysis

The implementations demonstrate different approaches to finding the nth prime:

Trial division: Test each candidate number for primality by checking divisibility up to √n. Simple but inefficient for large n.

Incremental sieve: Build a sieve incrementally as needed, only up to the required size. More memory efficient than pre-computing a large sieve.

Optimized trial division: Skip even numbers after 2, use 6k±1 optimization for checking factors. Balances simplicity with performance.

Time complexity varies: trial division is O(n√p) where p is the nth prime, while sieve approaches are O(n log log n) for the range needed.

Key Insights

  • The 10,001st prime (104,743) is relatively small compared to what might be expected
  • Most implementations use a sieve of Eratosthenes approach for efficiency
  • The search space needs to be large enough - typically around n×ln(n)×ln(n) gives good bounds
  • Even numbers >2 are never prime, allowing significant optimization
  • The pattern 6k±1 covers all potential primes except 2 and 3
  • Performance depends heavily on the primality test efficiency

Educational Value

This problem introduces fundamental concepts in:

  • Prime number generation and testing algorithms
  • The sieve of Eratosthenes as an efficient prime-finding method
  • The mathematical properties of prime distributions
  • Algorithm selection based on problem constraints
  • The trade-offs between time and space complexity
  • Understanding when to pre-compute vs. compute on-demand
  • The importance of mathematical bounds in algorithm design