10001st Prime
Determine the 10001st prime number, where primes are indexed as p_1 = 2, p_2 = 3, p_3 = 5,...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
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 \(10\,001\)st prime number
Problem 7: 10001st Prime
Mathematical Development
Definitions
Definition 1. A natural number is prime if its only positive divisors are 1 and . A natural number that is not prime is composite.
Definition 2. The prime-counting function denotes the number of primes satisfying . We write for the -th smallest prime.
Upper Bound on
Theorem 1 (Prime Number Theorem). As ,
Equivalently, as .
Theorem 2 (Rosser, 1941). For every integer ,
Reference. J. B. Rosser, “Explicit bounds for some functions of prime numbers,” Amer. J. Math. 63 (1941), 211—232. The proof relies on explicit bounds for the Chebyshev function and is taken here as established.
Proposition 1 (Sieve bound for ). The 10001st prime satisfies .
Proof. Since , Theorem 2 applies. We compute:
Hence . We set the sieve limit to provide a safety margin.
Correctness of the Sieve of Eratosthenes
Theorem 3 (Sieve of Eratosthenes). Let be an integer. Define the sieve procedure:
- Initialize a Boolean array with for all .
- Set .
- For each integer from to : if , set for every integer with .
Then for every integer , if and only if is prime.
Proof. We establish both directions.
Claim A (every composite is marked). Let be composite. Write with . Then . Let be the smallest prime factor of ; then . At iteration of the sieve, all multiples of starting from are marked false. Since is a multiple of and , the entry is set to false.
Claim B (no prime is marked). Let be prime. In step 3, could only be set to false during the iteration for some prime with . But marking begins at , and the values marked are , all of which are multiples of . Since is prime and , we have , so is never marked.
Corollary 1. Running the Sieve of Eratosthenes up to and extracting the -th entry equal to true (counting from ) yields for any with . By Proposition 1, this includes .
Editorial
The algorithm first chooses a proven upper bound that is guaranteed to contain the -th prime. It then runs the Sieve of Eratosthenes on , marks every composite starting from , and performs a final increasing scan through the sieve until the -th marked prime is reached. This is sufficient because the bound contains and the sieve leaves exactly the primes unmarked.
Theorem 5 (Algorithm correctness). NthPrime(n) returns the -th prime number for every .
Proof. If , then the algorithm uses the explicit bound , and the primes up to 20 are
so the -th prime certainly lies in .
If , the algorithm sets
By Theorem 2, , so again .
By Theorem 3, after the sieve phase the entries marked true are exactly the primes in . The final scan visits these integers in strictly increasing order and increments count once for each prime encountered. Therefore when count == n, the current value is exactly the -th prime. Hence the returned value is .
Numerical Evaluation
Proposition 2. The algorithm returns
Proof. By Proposition 1, it is enough to sieve up to . Running the algorithm on this interval shows:
- there are exactly primes strictly less than ;
- there are exactly primes less than or equal to .
Therefore is the -st prime.
Pseudocode
Algorithm: nth Prime by Sieve
Require: An integer n ≥ 1.
Ensure: The n-th prime number.
1: Choose an upper bound U that is guaranteed to contain the n-th prime.
2: Build a sieve on {2, 3, ..., U}, marking composites from p^2 onward for each prime base p.
3: Scan the surviving primes in increasing order, maintaining a counter c.
4: When c = n, return the current prime.
Complexity Analysis
Theorem 4 (Sieve complexity). The Sieve of Eratosthenes on runs in time and space.
Proof. Time. The total number of marking operations is
By Mertens’ second theorem (1874), , where is the Meissel—Mertens constant. Hence the sum is . The subsequent linear scan to locate costs , which is dominated.
Space. The Boolean array has entries, giving space.
Corollary 2. With by Theorem 2, the overall time complexity for finding is and space complexity is .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
const int N = 115000; // Rosser's bound: p_10001 < 10001*(ln 10001 + ln ln 10001) < 114320
vector<bool> sieve(N + 1, true);
sieve[0] = sieve[1] = false;
for (int i = 2; i * i <= N; i++)
if (sieve[i])
for (int j = i * i; j <= N; j += i)
sieve[j] = false;
int count = 0;
for (int i = 2; i <= N; i++) {
if (sieve[i] && ++count == 10001) {
cout << i << endl;
return 0;
}
}
return 0;
}
import math
def sieve_of_eratosthenes(limit):
is_prime = [True] * (limit + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
for j in range(i * i, limit + 1, i):
is_prime[j] = False
return [i for i in range(2, limit + 1) if is_prime[i]]
n = 10001
upper = int(n * (math.log(n) + math.log(math.log(n)))) + 100
primes = sieve_of_eratosthenes(upper)
print(primes[n - 1])