Online Prime Number Generator

Quick Presets:
Separator for copying

What is a prime number?

Aprime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The smallest prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29… The number 2 is the only even prime number – all other even numbers are divisible by two.

Numbers like 4 (= 2 × 2), 6 (= 2 × 3), or 15 (= 3 × 5) are not prime numbers – we call them composite numbers.

How does the generator work?

To list all prime numbers in a range, we use the Sieve of Eratosthenes – one of the oldest and most efficient algorithms, described by the Greek mathematician Eratosthenes of Cyrene around 240 BC.

The algorithm iteratively marks the multiples of each found prime number. What remains are the prime numbers. The entire calculation takes place directly in your browser – no data is sent to the server.

For random selection, the generator first builds a list of all prime numbers within the range and then randomly selects the desired count from it using a cryptographically secure generator (crypto.getRandomValues()).

Generator Features

  • All primes – lists every prime number in the specified range (max 10,000)
  • Random selection – selects N random prime numbers from the range (suitable for large ranges)
  • Sorting – sorts the results in ascending order
  • Separator – choose how numbers will be separated when copying
  • Quick Presets – most common ranges with one click

Where are prime numbers used?

Cryptography and Security

Prime numbers are the foundation of modern cryptography. Algorithms like RSA work on the principle that the product of two large prime numbers is easy to compute, but factoring them back into their prime components is computationally very difficult.

  • RSA encryption – keys are generated from two large prime numbers
  • Diffie-Hellman – key exchange over a prime modulus
  • Hashing functions – prime numbers as “magic constants” (SHA, MD5)

Mathematics and Science

  • Number theory – the fundamental building blocks of integers
  • Goldbach conjecture – every even number > 2 can be expressed as the sum of two primes (yet unproven)
  • Riemann hypothesis – one of Hilbert’s problems, concerning the distribution of prime numbers

Practical Applications

  • Hash tables – using a prime number for table size reduces collisions
  • Pseudorandom number generators – linear congruential generators with a prime modulus
  • Music and Rhythm – polyrhythms with prime-length cycles

Distribution of Prime Numbers

Prime numbers are irregularly distributed among natural numbers, but their density decreases with increasing range. This is described by the Prime Number Theorem: the number of primes up to N is approximately N / ln(N).

RangeNumber of Primes
1–104
1–10025
1–1,000168
1–10,0001,229
1–100,0009,592
1–1,000,00078,498

Sieve of Eratosthenes Step-by-Step

Range 2–30:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

1. Select 2, cross out its multiples: 4, 6, 8, 10, 12...
2. Select 3, cross out its multiples: 9, 15, 21, 27...
3. Select 5, cross out its multiples: 25...
4. √30 ≈ 5.5 → done

Primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29

Sieve of Eratosthenes in Code

JavaScript

function sieve(to) {
  const composite = new Uint8Array(to + 1);
  const primes = [];
  for (let p = 2; p <= to; p++) {
    if (composite[p]) continue;
    primes.push(p);
    for (let j = p * p; j <= to; j += p) composite[j] = 1;
  }
  return primes;
}

Python

def sieve(n):
    composite = bytearray(n + 1)
    primes = []
    for p in range(2, n + 1):
        if not composite[p]:
            primes.append(p)
            for j in range(p * p, n + 1, p):
                composite[j] = 1
    return primes

Frequently Asked Questions (FAQ)

Is 1 a prime number? No. The number 1 is not traditionally considered a prime number. The reason is mathematical – if we considered 1 a prime number, the fundamental theorem of arithmetic (unique prime factorization) would lose its validity.
Is 2 a prime number? Yes. The number 2 is the smallest and also the only even prime number. All other even numbers are divisible by two, and thus composite.
How many prime numbers exist? Infinitely many. Euclid proved this around 300 BC with an elegant proof by contradiction: suppose there is a finite number of primes. Their product + 1 cannot then be divisible by any of them – thus it is a new prime number, a contradiction.
What are twin primes? Twin primes are pairs of prime numbers that differ by 2, for example (3, 5), (11, 13), (17, 19), (41, 43). Whether infinitely many of them exist is still an unsolved problem in mathematics (the Twin Prime Conjecture).
How fast does the algorithm work? The Sieve of Eratosthenes has a time complexity of O(n log log n). For a range up to 10 million, the calculation typically takes less than 100 ms in the browser.