Prime number checker
Is it prime? The answer, with the factor that proves it
Type a whole number. The example is 97.
–
- Nearest prime below
- –
- Nearest prime above
- –
Working, step by step
What makes a number prime
A prime number is a whole number bigger than 1 whose only factors are 1 and itself. 2, 3, 5, 7, 11 and 13 are prime. 9 is not, because 9 = 3 × 3. The number 1 is not prime: it has only one factor. 2 is the only even prime, since every other even number divides by 2.
How the page checks
It tries to divide your number by 2, by 3, and then by every number of the form 6k − 1 and 6k + 1 (5, 7, 11, 13, 17, 19 and so on), stopping at the square root. If any of them divides exactly, the number is not prime and the page shows that factor as proof, for example 91 = 7 × 13. If none does, the number is prime. Stopping at the square root is safe: if a number had a factor above its square root, the other factor in the pair would be below it and would already have been found.
How big a number can it check?
Up to 9,007,199,254,740,991, the largest whole number a browser stores exactly. For the biggest numbers the check makes up to about thirty million divisions, which a phone may take a second or two to do.
Nearest primes
The nearest primes below and above your number are found the same way, one number at a time. They are handy for spotting twin primes, such as 11 and 13, which are two apart.
Formula and sources
- Prime and composite numbers: Common Core 4.OA.B.4.
- Trial division up to √n, skipping multiples of 2 and 3 (the 6k ± 1 rule): Knuth, The Art of Computer Programming, volume 2, section 4.5.4.
- Checked in test_maths.js against the list of primes below 1,000 (168 primes) and known cases: 97 prime; 91 = 7 × 13; 1 and 0 not prime; 2,147,483,647 prime; 1,000,000,007 prime; 9,007,199,254,740,991 = 6,361 × 69,431 × 20,394,401.