Number Theory & Bases

Prime Number Checker and Prime Numbers List Generator

Enter a whole number to check if it is prime, or switch to List primes in a range to get every prime number between two limits.

Try
What to do

Up to 200 digits. Type the number in full, for example 2305843009213693951 for 2⁶¹ − 1.

Result

Is 9973 prime?: Prime

Is 9973 prime?

Prime

9973 has no divisors other than 1 and itself.

Next prime34 more than 9973
10,007
Previous prime6 less than 9973
9967
Test used
Trial division by every prime up to √n ≈ 99.86
Certainty
Proven prime
Position among primes
The 1229th prime
Twin prime?
No: neither n − 2 nor n + 2 is prime

9973 is a prime number. The nearest primes are 9967 and 10,007.

Remainders of 9973 divided by each prime tested
Prime pRemainderDivides?
21No
31No
53No
75No
117No
132No
1711No
1917No
2314No
2926No
3122No
3720No
4110No
4340No
479No
539No
592No
6130No
6757No
7133No
7345No
7919No
8313No
895No
9779No
Step-by-step working (3 steps)
  1. Find how far to search

    If 9973 had a factor other than 1 and itself, one factor would be at most √9973 ≈ 99.86. So only the primes up to 99 need testing.

    9973≈99.86\sqrt{9973} \approx 99.86
  2. Divide by each prime

    None of the 25 primes from 2 to 97 divides 9973: every remainder is non-zero (see the table).

  3. Conclusion: prime

    9973 has no prime factor up to its square root, so its only divisors are 1 and 9973.

There are 25 primes from 1 to 100 and 168 below 1,000. Small numbers are tested by trial division up to n\sqrt{n}, large ones by the Miller–Rabin test, and a composite answer shows its smallest prime factor.

By Hira Noor, mathematicianLast reviewed Free, no sign-up, runs in your browserReport a problem

How to use this calculator

  1. Choose Is it prime? to test one number, or List primes in a range to get a prime numbers list.
  2. To test a number, type a whole number with up to 200 digits, such as 9973 or 2305843009213693951. Commas between digit groups are fine.
  3. Read the verdict: prime, composite, or probable prime for some very large numbers. For a composite number, the smallest prime factor and the factorization are the proof.
  4. To list primes, enter where the range starts and ends, up to 10,000 numbers apart and ending at 10¹² or below. The table numbers each prime and marks twin and Mersenne primes; Download CSV saves it.
  5. Open the steps to see the remainders from trial division, the Miller–Rabin sequence, or how the sieve built the list.

Formulas

Definition of a prime

p>1 is prime⇔its only positive divisors are 1 and pp > 1 \text{ is prime} \iff \text{its only positive divisors are } 1 \text{ and } p

Plain-text formula: p > 1 is prime ⇔ its only positive divisors are 1 and p

A whole number greater than 1 that is not prime is composite. The numbers 0 and 1 are neither.

Trial division bound

n=ab,1<a≤b⇒a≤nn = ab,\; 1 < a \le b \;\Rightarrow\; a \le \sqrt{n}

Plain-text formula: n = ab, 1 < a ≤ b ⇒ a ≤ √(n)

If n has a factorization into two factors greater than 1, the smaller one is at most √n. So testing the primes up to √n is enough, and the same bound tells the sieve when to stop.

Miller–Rabin test

n−1=2sd,ad≡1 or a2rd≡−1(modn) for some 0≤r<sn - 1 = 2^{s} d,\quad a^{d} \equiv 1 \;\text{ or }\; a^{2^{r} d} \equiv -1 \;(\mathrm{mod}\; n) \text{ for some } 0 \le r < s

Plain-text formula: n − 1 = 2^s d, a^d ≡ 1 or a^(2^r d) ≡ −1 (mod n) for some 0 ≤ r < s

aa
the base (2, 3, 5, …, 41)
dd
the odd part of n − 1

Every odd prime satisfies this for every base a. A composite number fails it for at least three quarters of the bases, and below 3.3×10243.3 \times 10^{24} the 13 prime bases 2 to 41 always expose it.

Prime number theorem

π(n)≈nln⁡n\pi(n) \approx \frac{n}{\ln n}

Plain-text formula: π(n) ≈ n/(ln n)

π(n)\pi(n)
the number of primes less than or equal to n

π(n) counts the primes up to n. From n = 17 upward the estimate is always a little low: π(1,000) = 168, while 1,000 / ln 1,000 ≈ 145.

Reading the result

A composite verdict is always certain: the calculator either shows a factor or a Miller–Rabin witness, a base that a prime could never fail. A prime verdict is a proof for every number below 3.3×10243.3 \times 10^{24} and for Mersenne numbers 2p−12^p - 1, which get the Lucas–Lehmer test.

Above that bound, a number that passes is reported as a probable prime. The Baillie–PSW combination used here has been checked against every number below 2642^{64} and has no known counterexample, but it has not been proven correct in general.

In list mode every prime is exact, because a sieve only removes numbers that have a factor. Position is where the prime falls in the sequence 2, 3, 5, 7, … (709 is the 127th prime). A twin prime has another prime 2 away, and a Mersenne prime is one less than a power of 2.

Prime numbers list from 1 to 100

There are 25 prime numbers from 1 to 100: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 and 97. 2 is the only even prime, and 97 is the largest prime below 100.

Eight pairs of them are twin primes, two apart: (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61) and (71, 73). Three are Mersenne primes: 3, 7 and 31.

Primes from 1 to 100, by tens
RangePrime numbersCount
1 to 102, 3, 5, 74
11 to 2011, 13, 17, 194
21 to 3023, 292
31 to 4031, 372
41 to 5041, 43, 473
51 to 6053, 592
61 to 7061, 672
71 to 8071, 73, 793
81 to 9083, 892
91 to 100971

Prime numbers from 101 to 1,000

There are 143 primes from 101 to 1,000, so 168 prime numbers are below 1,000. Each block of 100 holds fewer primes than the first: 21 from 101 to 200, but only 14 from 901 to 1,000.

For any other stretch, such as the primes to 200, from 300 to 400, or the 1,229 primes up to 10,000, choose List primes in a range above and download the table as CSV.

Primes from 101 to 1,000, by hundreds
RangePrime numbersCount
101 to 200101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 19921
201 to 300211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 29316
301 to 400307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 39716
401 to 500401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 49917
501 to 600503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 59914
601 to 700601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 69116
701 to 800701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 79714
801 to 900809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 88715
901 to 1,000907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 99714

How to check if a number is prime

Divide the number by each prime up to its square root. If none of them divides it exactly, it is prime; the first one that does is its smallest prime factor, and the prime factorization calculator finds the rest. Checking beyond √n is never needed, because a factor larger than √n pairs with one smaller than √n.

Rule out the easy cases first. An even number above 2 is divisible by 2, a number whose digits add up to a multiple of 3 is divisible by 3, and one ending in 0 or 5 is divisible by 5. After that, every prime above 3 is one more or one less than a multiple of 6 (the form 6k ± 1), so you only need to try 5, 7, 11, 13, 17, 19 and so on. Every prime above 5 ends in 1, 3, 7 or 9.

For example, 91 looks prime, but √91 ≈ 9.5 and 91 ÷ 7 = 13, so 91 = 7 × 13 is composite. 97 is prime: the primes up to √97 ≈ 9.8 are 2, 3, 5 and 7, and each leaves a remainder.

By hand this works well up to a few thousand. For a 20-digit number it could take hundreds of millions of divisions, so computers switch to the Miller–Rabin test. This calculator does so above about 1,000,000.

In code, the same method is a short loop: if n < 2 it is not prime; if n < 4 it is prime; if n % 2 == 0 or n % 3 == 0 it is composite. Otherwise try i = 5, 11, 17, … while i * i <= n, and return composite if n % i == 0 or n % (i + 2) == 0. That loop works in Python, JavaScript, Java or C++ and is fast up to about 10¹². Miller–Rabin replaces it for bigger numbers and needs only fast powers mod n, which the modulo calculator shows step by step.

The sieve of Eratosthenes

To make a list of primes rather than test one number, use the sieve of Eratosthenes. Write out the numbers from 2 to your limit. Circle 2 and cross out every later multiple of 2. Circle the next number still standing (3) and cross out its multiples, then do the same for 5 and 7. Once the next number to circle is larger than the square root of the limit, every number left is prime.

For 1 to 100 only the multiples of 2, 3, 5 and 7 need crossing out, since √100 = 10. The list mode above runs the same sieve on just the range you ask for (a segmented sieve), so it can list the primes near 10¹² without sieving everything below them.

How many prime numbers are there?

There are infinitely many. Euclid’s proof: multiply any finite list of primes together and add 1. The result leaves a remainder of 1 when divided by each prime on the list, so its own prime factors are primes the list missed.

Primes thin out slowly as numbers grow. The prime number theorem says the count up to n is close to n / ln n, so roughly 1 in every ln n numbers near n is prime: about 1 in 7 near 1,000 and about 1 in 14 near 1,000,000.

How many primes there are up to n
nPrimes up to n, π(n)Estimate n / ln n
1044.3
1002521.7
1,000168144.8
10,0001,2291,085.7
100,0009,5928,685.9
1,000,00078,49872,382.4

Why 0, 1 and negative numbers are not prime

A prime needs exactly two positive divisors. 1 has only one (itself), and 0 has infinitely many, because every number divides 0. Excluding 1 is also what keeps factorizations unique: otherwise 6 = 2 × 3 = 1 × 2 × 3 = 1 × 1 × 2 × 3, and so on.

Primes are defined as positive. In more advanced algebra, −7 is called a prime element because it is a unit (−1) times the prime 7, but it is not a prime number. The calculator reports such inputs as not prime and analyzes their absolute value.

Worked example

Is 221 prime?

Decide whether 221 is prime by trial division.

  1. √221 ≈ 14.87, so only the primes 2, 3, 5, 7, 11 and 13 need testing.
  2. 221 is odd, its digit sum 5 is not a multiple of 3, and it does not end in 0 or 5.
  3. 221 ÷ 7 = 31 remainder 4 and 221 ÷ 11 = 20 remainder 1.
  4. 221 ÷ 13 = 17 exactly, so 13 is a factor.

Answer: 221 is composite: 221 = 13 × 17. Its smallest prime factor is 13, and the nearest primes are 211 and 223.

Frequently asked questions

How many prime numbers are there from 1 to 100?

There are 25: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 and 97. There are 168 primes below 1,000 and 1,229 below 10,000.

Is 1 a prime number?

No. A prime has exactly two positive divisors, and 1 has only one. Mathematicians agreed to exclude 1 so that every number has a unique prime factorization.

Is 2 a prime number?

Yes. 2 is the smallest prime and the only even prime, since every larger even number is divisible by 2.

Is 7 a prime number?

Yes. 7 is divisible only by 1 and 7, and it is the 4th prime, after 2, 3 and 5.

Why is 39 not a prime number?

39 = 3 × 13. Its digits add up to 12, a multiple of 3, which is the quick sign that 3 divides it.

What is the 127th prime number?

709. The 100th prime is 541 and the 1,000th is 7,919. The checker shows the position of any prime up to 1,000,000, and the list mode numbers every prime in ranges ending at or below 10,000,000.

What is the fastest way to check if a number is prime?

For a small number, divide it by the primes up to its square root; for 97 that is only 2, 3, 5 and 7. For large numbers, the Miller–Rabin test is much faster than looking for factors, and this calculator answers instantly for numbers up to 200 digits.

How can a test say a large number is composite without finding a factor?

The Miller–Rabin test checks a property that every prime has. If a number fails it for some base, that base is a witness and the number cannot be prime, even though no factor is known.

What is a probable prime?

A number that passes tests every prime passes, but without a formal proof. Here that only happens above 3.3×10243.3 \times 10^{24}, with the Baillie–PSW test, which no composite number is known to pass.

Why is 561 not prime even though it passes the Fermat test?

561 = 3 × 11 × 17 is a Carmichael number: a560≡1(mod561)a^{560} \equiv 1 \;(\mathrm{mod}\; 561) for every base a coprime to 561. The Miller–Rabin test is stronger and catches it with base 2.

How big a number can I check?

Up to 200 digits. The answer is usually instant; numbers near the limit take a fraction of a second because the calculator also searches for the next and previous primes. Lists cover up to 10,000 numbers at a time, ending at 10¹² or below.

LCM and GCF Calculator

LCM and GCF of up to 10 numbers with steps: multiples, prime factors, Euclid’s algorithm and a coprime check.

Number Theory & Bases

Mod Calculator

a mod n with steps, negative numbers, power mod and modular inverses.

Number Theory & Bases

Factorial Calculator

Exact n! with steps and every digit, plus digit count, trailing zeros, n!! and Stirling’s approximation.

Number Theory & Bases

Binary Calculator

Binary addition, subtraction and bitwise operations with steps, at 8 to 64 bits, signed or unsigned.

Number Theory & Bases

Scientific Calculator

Type or tap expressions with exact fractions, memory, variables, constants and complex mode.

Tools

Sources and further reading

  1. A000040: The prime numbers (opens in a new tab), The On-Line Encyclopedia of Integer Sequences (OEIS Foundation)
  2. Strong pseudoprimes to twelve prime bases (J. Sorenson and J. Webster, Mathematics of Computation 86, 2017), American Mathematical Society
  3. Prime Numbers: A Computational Perspective (R. Crandall and C. Pomerance, 2nd ed.), Springer
  4. Lucas pseudoprimes (R. Baillie and S. S. Wagstaff Jr., Mathematics of Computation 35, 1980), American Mathematical Society

Results are calculated in your browser with methods checked by automated tests, and are intended for learning and general use. For engineering, medical, financial, legal or safety-critical decisions, verify important values independently and follow the rounding rules your work requires. How accurate are these results?