MathsNumber Theory

Prime Number
checker.

Find out if a whole number is prime, see its factors, and find the primes next to it.

How it's worked out

A prime is a whole number above 1 whose only divisors are 1 and itself. Every other number above 1 is composite: it splits into smaller factors. The number 1 is neither, since it has just one divisor.

Trial division tests the primes in order. You only need to go as far as the square root of the number. If n splits as a times b, the two parts cannot both be bigger than the root, because then their product would be bigger than n. So if no prime up to the root divides n, n is prime.

n\displaystyle n=a×b\displaystyle {}= a \times b
a\displaystyle a≤n\displaystyle {}\le \sqrt{n}≤b\displaystyle {}\le b

For 97 the root is a little under 10, so the primes to try are 2, 3, 5, 7. None of them leaves a remainder of 0, so 97 is prime. Up to one trillion the tool works this way, and the steps show each division or a count of the primes tried.

Above one trillion it uses the Miller–Rabin test. It writes n − 1 as a power of 2 times an odd number, then runs a fast power test for each of 13 bases, the primes from 2 to 41. A prime passes for every base. A composite almost always fails. The smallest odd composite that passes all 13 is 3,317,044,064,679,887,385,961,981, found by Sorenson and Webster. That has 25 digits, so for every number of up to 24 digits the test gives a proof, not a guess. For the largest 24-digit prime, n − 1 is 2 times the odd number 499,999,999,999,999,999,999,871.

n−1\displaystyle n - 1=2r×d\displaystyle {}= 2^{r} \times d

The check at the end substitutes back. For a composite it multiplies the factor pair to get n again. For a prime it uses Fermat's little theorem: when p is prime and a is not a multiple of p, a to the power p − 1 leaves a remainder of 1 when divided by p.

ap−1\displaystyle a^{p-1}≡1(modp)\displaystyle {}\equiv 1 \pmod{p}

Worked examples

Maya checks whether 91 is prime

A classmate tells Maya that 91 is prime. It is odd, it does not end in 5, and its digits add to 10, so the quick rules for 2, 3 and 5 all pass it. The root of 91 is between 9 and 10, so she only needs the primes up to 9.

Dividing 91 in turn: 2 leaves 1, 3 leaves 1, 5 leaves 1, 7 leaves 0. The remainder 0 means 7 goes in exactly, 13 times. So 91 is composite, and its factors are 7 × 13. The check multiplies them back: 7 times 13 is 91. The next prime after 91 is 97.

Open this example in the tool

Dev checks a modulus before he uses it

Dev writes code that keeps its numbers small by taking remainders on division by 1,000,000,007. That only works the way he wants if the number is prime. Its root rounds down to 31,622, and there are 3,401 primes up to that point.

None of those 3,401 primes divides it, so it is prime, and the working ends with Fermat's check. It also shows how close the next primes are: 999,999,937 is 70 below, and 1,000,000,009 is just 2 above. Those two are twin primes, a pair two apart.

Open this example in the tool

Mr. Okafor lists the primes from 1 to 100

For a class poster, Mr. Okafor lists every prime up to 100. The tool sieves the range: it crosses out the multiples of 2, 3, 5 and 7, the primes up to the root of 100, and what is left is 25 primes.

The table gives the gap before each prime. The widest gap is 8, from 89 to 97. There are 8 twin pairs: 3 and 5, 5 and 7, 11 and 13, 17 and 19, 29 and 31, 41 and 43, 59 and 61, 71 and 73. He can export the table as a file for the poster.

Open this example in the tool

Reading your result

Composite answers show the factors in full up to one trillion. Above that, the tool shows the smallest prime factor if it is below one million. A number with two large prime factors is still proved composite, but finding those factors is a much harder job.

Gaps between primes grow slowly. Below 100 there are 25 primes, so one number in four is prime. Of the first million numbers, 78,498 are prime (OEIS A006880), which is fewer than one in twelve. The gap figures and the grid of 100 numbers show this thinning near your own number.

Special kinds appear in the tiles when they apply. A twin prime has another prime 2 away. A Sophie Germain prime p has 2p + 1 prime too, and that 2p + 1 is a safe prime: 5 and 23 are both. A Mersenne prime is one less than a power of 2, such as 127. A Fermat prime is one more than 2 raised to a power of 2. Only five are known: 3, 5, 17, 257 and 65,537.

Common mistakes

  • Calling 1 a prime. A prime has exactly two divisors, and 1 has one. Counting it would let every number factor in endless ways, such as 6 = 2 × 3 × 1 × 1.
  • Stopping at the quick rules. The tests for 2, 3 and 5 miss numbers like 91, 119 and 133, which are 7 times 13, 7 times 17 and 7 times 19.
  • Trusting a Fermat test alone. 561 passes Fermat's test for every base that shares no factor with it, yet it is 3 × 11 × 17. Numbers like this are Carmichael numbers. Trial division finds its factor 3 at once.
  • Testing every number up to n. You only need the primes up to the square root. Past that, any factor you find pairs with a smaller one you have already tried.

Questions people ask

A prime has exactly two different divisors, and 1 has only one. Leaving 1 out also keeps prime factorization unique: if 1 counted, 12 could be written as 2 × 2 × 3, or 1 × 2 × 2 × 3, and so on forever.

Sources