Prime Number Generator

Generate prime numbers instantly using the classical Sieve of Eratosthenes. Choose between range-based generation (up to 10 million) or first N primes. View prime list, count, sum, and download results as text.

Range: 2 to N
First N primes
Maximum limit: 10,000,000 for performance reasons. Larger values will be capped.
? Primes up to 100
? Primes up to 1000
✨ First 50 primes
? First 200 primes
? Primes up to 500 (twin primes demo)
Privacy first: All calculations run locally in your browser. No data is sent to any server. You can verify this by checking the network activity in your browser's developer tools (F12).

The Sieve of Eratosthenes: An Ancient Algorithm

The Sieve of Eratosthenes is one of the most efficient classical algorithms for generating all primes up to a given limit. Attributed to the Greek mathematician Eratosthenes of Cyrene (c. 276–194 BCE), it works by iteratively marking multiples of each prime starting from 2. The remaining unmarked numbers are prime. This algorithm runs in \( O(n \log \log n) \) time and uses \( O(n) \) memory, making it optimal for ranges up to tens of millions in modern browsers.

For a given limit \( N \), the Sieve of Eratosthenes eliminates composites by iterating \( p = 2,3,5,\dots \) and marking \( p^2, p^2 + p, p^2 + 2p, \dots \) as composite.

Algorithm Complexity Analysis

The time complexity of \( O(n \log \log n) \) and space complexity of \( O(n) \) make the Sieve of Eratosthenes highly efficient for generating all primes up to moderate bounds (typically ≤ 10⁸ in optimized implementations). The \( \log \log n \) factor emerges from the harmonic series of primes. Our browser implementation includes standard optimizations: skipping even numbers (a simple form of wheel factorization), starting marking from \( p^2 \), and using typed arrays for memory efficiency.

How This Tool Works

Our implementation uses a typed Uint8Array to store boolean flags (0/1) for primality up to the specified limit. For range mode, the classical sieve marks composites starting from \( p^2 \) to avoid redundant work. For count mode (first N primes), the tool dynamically estimates an upper bound using the prime number theorem: the nth prime is roughly \( n \ln n \). It then runs a segmented approach or simply extends the sieve until enough primes are found. The result is a sorted list of primes, accompanied by statistics: count, largest prime, sum, and density \( \pi(N)/N \).

Performance optimizations include: skipping even numbers after 2 (a basic wheel optimization), using bit-level operations where feasible, and limiting maximum range to 10 million to prevent browser unresponsiveness. For count mode, we cap at 100,000 primes (largest ~1,299,709) which is processed within seconds. More advanced wheel factorizations (mod 30, 210) are possible but increase code complexity with diminishing returns for typical educational use.

Applications of Prime Generation

  • Cryptography: Generating prime candidates for RSA keys, Diffie-Hellman parameters. Note: For educational RSA key generation, primes of hundreds of digits are required, which is beyond this tool's performance scope. Production cryptographic systems use probabilistic tests (Miller-Rabin) and specialized algorithms for large primes.
  • Mathematics Education: Visualizing prime gaps, twin primes, and Goldbach's conjecture.
  • Competitive Programming: Precomputing primes for factorization, primality tests, or combinatorics.
  • Research: Analyzing prime distribution, testing conjectures like Legendre's or Bertrand's postulate.

Prime Number Theorem & Distribution

The Prime Number Theorem (PNT) describes the asymptotic density of primes: the number of primes less than or equal to \( x \) is approximately \( \frac{x}{\ln x} \). Our generator displays the empirical density \( \pi(N)/N \), allowing you to verify the PNT for different ranges. For example, \( \pi(10^6) = 78,498 \) while \( 10^6 / \ln(10^6) \approx 72,382 \). The ratio approaches 1 as N increases.

The PNT gives the first-order approximation \( \pi(x) \sim \frac{x}{\ln x} \). More accurate approximations include \( \frac{x}{\ln x - 1} \) and the logarithmic integral \( \operatorname{Li}(x) = \int_2^x \frac{dt}{\ln t} \). The error term in the PNT remains an active research area connected to the Riemann Hypothesis.

Range (N) π(N) (primes ≤ N) Approx. N/ln(N) Density π(N)/N
102 25 21.7 0.250
103 168 144.8 0.168
104 1,229 1,085.7 0.1229
105 9,592 8,685.9 0.0959
106 78,498 72,382.4 0.0785

Data sourced from OEIS sequence A006880 and prime number theorem approximations.

Case Study: Twin Prime Conjecture

Generate primes up to 10,000 and count how many twin prime pairs (p, p+2) exist. The tool's output can be analyzed manually: for N=10,000 there are 205 twin prime pairs, with the largest being (9929, 9931). This real-time exploration helps students appreciate unsolved problems in number theory. The Twin Prime Conjecture states there are infinitely many such pairs – though unproven, computational evidence strongly supports it. Use this tool to explore prime gaps and twin prime distribution patterns.

Step-by-Step Usage

  1. Select mode: Range (2 to N) or First N primes.
  2. Enter the desired limit or prime count.
  3. Click Generate Primes – the sieve will compute the list instantly for moderate sizes.
  4. View the formatted prime list (multi-column), statistics panel, and download the results as a plain text file.
  5. Analyze the results: Use the generated list and statistics to observe prime distribution patterns, such as prime gaps, twin primes, or verify the Goldbach conjecture for small even numbers.

Frequently Asked Questions

The tool is capped at 10,000,000 for range mode and 100,000 primes for count mode to ensure smooth browser performance. You can safely generate primes up to 10 million within a few seconds. These limits balance performance with educational usefulness while preventing browser unresponsiveness.

For generating all primes up to a moderate bound (≤ 10⁸), the Sieve of Eratosthenes is optimal. For larger ranges, segmented sieves or the Sieve of Atkin may be faster, but they are more complex. Our tool focuses on clarity and efficiency for typical educational use. The implementation includes standard optimizations like skipping even numbers and starting from p².

We estimate an upper bound using the inequality pn < n(ln n + ln ln n) for n ≥ 6 (Dusart 1999 bound). Then we run the sieve up to that bound. If not enough primes are found, we incrementally extend the range. This approach guarantees correctness without excessive over-allocation. The bound is known to be reliable for n ≥ 6.

Yes, you can use them for learning and prototyping. However, for production cryptographic systems, you should rely on cryptographically secure random prime generation (e.g., using Miller-Rabin with many bases). This tool is educational but the primes are mathematically correct. Real cryptographic systems require primes with hundreds of digits, which is beyond this tool's practical scope.

Prime density π(N)/N is the fraction of numbers up to N that are prime. As N increases, primes become rarer; the Prime Number Theorem says this density decays like 1/ln(N). Our generator computes the exact density for your chosen N, illustrating this fundamental law. In "first N primes" mode, the displayed density is π(p_max)/p_max where p_max is the largest prime found.

For the given input within the tool's limits, the results are mathematically accurate. The Sieve of Eratosthenes is a deterministic algorithm that correctly identifies all primes up to the given bound. The implementation uses JavaScript's standard 64-bit floating point numbers (IEEE 754) for integer arithmetic, which maintains exact integer precision up to 2^53. Our limits are well within this range, ensuring accuracy. The tool has been validated against known prime tables.
References: MathWorld: Sieve of Eratosthenes; H. Davenport, "Multiplicative Number Theory"; Prime Number Theorem (Wikipedia); OEIS A006880 (number of primes ≤ 10^n); P. Dusart, "The kth prime is greater than k(ln k + ln ln k - 1) for k ≥ 2"; T. H. Cormen et al., "Introduction to Algorithms" (Sieve analysis); Algorithm optimized based on known implementations from CP-algorithms and Project Euler.