Online Prime Number Generator
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).
| Range | Number of Primes |
|---|---|
| 1–10 | 4 |
| 1–100 | 25 |
| 1–1,000 | 168 |
| 1–10,000 | 1,229 |
| 1–100,000 | 9,592 |
| 1–1,000,000 | 78,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