Prime Number Generator
List the prime numbers in a range or choose a random subset without repetition. Set From and To, select the mode and click Generate. Random selection also lets you choose Count and Sort results.
The difference between the bounds cannot exceed 10,000,000, and a result can contain at most 10,000 primes. The upper bound must not exceed JavaScript’s maximum safe integer, but values near that bound can still take substantial time to process. A narrow range high on the number line is not necessarily quick.
What is a prime number?
A prime 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?
The tool marks composite values in the selected interval using divisors up to the square root of the upper bound. The unmarked integers greater than one are primes. This is a range-based sieve; random mode still calculates the prime list before selecting from it.
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
What this list is useful for
Use it for divisibility exercises, checking small prime tables and creating mathematical test data. It is not a cryptographic key generator: a list of small primes or a cryptographically sourced selection does not satisfy the requirements for RSA key generation.
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
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. If a list contained every prime, their product plus one would not be divisible by any prime in that list. It is either prime itself or has a prime factor missing from the list, giving a contradiction.What are twin primes?
Twin primes are pairs of prime numbers that differ by 2, such as (3, 5), (11, 13), (17, 19) and (41, 43).How fast does the algorithm work?
Speed depends on both the bounds and your device. The tool checks multiples of divisors up to the square root of the upper bound, so a high upper limit can take longer even for a narrow interval.For random integers that need not be prime, use the random number generator.