cipher.ink Write a message

Primes · Prime numbers

Is it prime?

Type a number of up to 300 digits and see whether it's prime, what its factors are if not, and every curious property it has.

Nothing you type here leaves your browser. The number goes in the page address only if you share it.

Type a number of up to 300 digits and this prime number checker tells you whether it's prime. If it isn't, you get its prime factors. For any number, you also get the primes on either side of it and a list of every curious property it has. The check is exact for numbers below 3.3 × 1024. Above that it's a test that no composite number is known to fool in practice. Everything runs in your browser, with no server involved.

What counts as a prime

A prime is a whole number greater than 1 whose only divisors are 1 and itself. The first few are 2, 3, 5, 7, 11 and 13, and there are 25 below 100, 9,592 below 100,000 and 78,498 below a million (scripts/examples/primes-personality.mjs counts them with the same sieve the sieve page animates). A number greater than 1 that isn't prime is composite, and the fundamental theorem of arithmetic says each composite number is a product of primes in exactly one way, apart from the order. That uniqueness is why mathematicians settled the question of 1.

Why 1 isn't prime

If 1 were prime, then 6 = 2 × 3 = 1 × 2 × 3 = 1 × 1 × 2 × 3, and "the" prime factorization of 6 wouldn't exist. Rather than add "except for ones" to every theorem, the convention is that a prime must have exactly two divisors, and 1 has one. It wasn't always the rule. Chris Caldwell and Yeng Xiong's 2012 history, What is the smallest prime?, follows how the answer moved over the centuries as the idea of a number changed. Zero isn't prime either. Every number divides it, so it has far more than two divisors.

How the check works

For small numbers the page divides by the small primes, which settles most of them at once. Anything that survives goes through the Miller–Rabin test. Write n − 1 as 2s · d with d odd, pick a base a, and compute ad modulo n, then square it up to s times. If n is prime, the sequence must either start at 1 or hit −1 (that is, n − 1) before it ends. If it does neither, a is a witness: a number that proves n is composite without giving a single factor. Try 561 or 1105 in the box above and the card names the witness it found.

A single base can be fooled by a composite number, so the page tries 13 bases, the first 13 primes (2 through 41). Gerhard Jaeschke worked out in 1993 that the first few primes are enough for small numbers, and Jonathan Sorenson and Jonathan Webster showed in 2015 that no composite number below 3,317,044,064,679,887,385,961,981 passes all of the first 13. Their search found that the smallest composite that passes the first 12 is 318,665,857,834,031,151,167,461. Base 41 catches it, and this site's tests check exactly that. Above that bound the 13 bases are no longer a proof. Nobody has ever found a composite that slips through all of them, and for random inputs the chance is astronomically small, so the card says "probable prime" and means it carefully. On the RSA key-size page you can generate 1,024-bit probable primes and see how real software does the same.

Factoring is a different and harder job than testing. To factor, the page tries Pollard's rho (in Richard Brent's faster form), which finds a factor p in about the square root of p steps. That's quick when the smallest factor has fewer than about 10 digits, and hopeless for the two 150-digit primes of a real RSA key. When the page gives up, it says so and shows what it did find. Telling prime from composite is easy, while finding the factors of a composite is the problem RSA's security rests on. The RSA toy lets you watch that gap at small scale.

What the labels mean

Below the verdict, the card lists everything it knows about your number, and each label opens to its definition. Here are the first few numbers for each, computed by the page's own code (scripts/examples/primes-personality.mjs):

LabelWhat it isFirst examples
Twin prime2 away from another prime3, 5, 7, 11, 13, 17, 19, 29
Cousin, sexy prime4 away, or 6 away, from another primecousin: 3, 7, 11, 13, 17, 19; sexy: 5, 7, 11, 13, 17
Sophie Germain primep and 2p + 1 are both prime2, 3, 5, 11, 23, 29, 41, 53
Safe primep = 2q + 1 with q prime5, 7, 11, 23, 47, 59, 83, 107
Emirpreversed digits give a different prime13, 17, 31, 37, 71, 73, 79, 97
Palindromic primereads the same backwards11, 101, 131, 151, 181, 191, 313, 353
Mersenne prime2k − 13, 7, 31, 127, 8191
Fermat prime22m + 13, 5, 17, 257 (and 65,537; no others are known)
Proth primek · 2m + 1, k odd and below 2m3, 5, 13, 17, 41, 97, 113, 193
Happy primerepeatedly summing the squares of the digits reaches 17, 13, 19, 23, 31, 79, 97, 103
Circular primeevery rotation of the digits is prime11, 13, 17, 31, 37, 71, 73, 79
Chen primep + 2 is prime or a product of two primes2, 3, 5, 7, 11, 13, 17, 19
Carmichael numbera composite that passes the Fermat test for every base561, 1105, 1729, 2465, 2821, 6601
Perfect numberequals the sum of its proper divisors6, 28, 496, 8128
Taxicab numbera sum of two cubes in two ways1729, 4104, 13832

Two of these have stories. Sophie Germain used her primes in the early 1800s in a partial attack on Fermat's Last Theorem, and the safe-prime form matters for Diffie–Hellman, where a group built on a safe prime has no small subgroups for an attacker to exploit. And 1729 is the number G. H. Hardy said he found dull on a taxi ride to visit the hospitalized Ramanujan, who replied that it was the smallest number that is the sum of two cubes in two ways (13 + 123 and 93 + 103). The card finds both, and it also marks 1729 as a Carmichael number.

Carmichael numbers: composites that look prime

Fermat's little theorem says that if n is prime then an−1 ≡ 1 (mod n) for every a not divisible by n. It's tempting to use that as a primality test, and for most composites it works. Carmichael numbers are the composites that pass it for every base coprime to them, the smallest being 561 = 3 × 11 × 17. Korselt's criterion characterizes them: the number is squarefree, and for each prime factor p, p − 1 divides n − 1. For 561, 2, 10 and 16 all divide 560. In 1994 Alford, Granville and Pomerance proved there are infinitely many. Miller–Rabin is stronger than the plain Fermat test and sees through every one of them, which is why the quiz on the prime-or-not page ends with them.

Where prime testing shows up in cipher.ink

It doesn't, in the product. A message you seal here uses AES and PBKDF2, and neither involves a prime (AES-GCM, PBKDF2). The primes on this site belong to the public-key side of the story. RSA, Diffie–Hellman and ElGamal all start by finding large primes, and the test you just used is the one browsers and OpenSSL run to find them.

Common questions

Is 1 a prime number?

No. A prime has exactly two divisors, and 1 has one. Counting it would break the unique factorization of every other number.

Is 2 really prime, since it's even?

Yes. Its only divisors are 1 and 2. It's the only even prime, because every other even number is also divisible by 2.

How can a number be a "probable" prime?

Above 3.3 × 1024, passing the 13-base Miller–Rabin test is strong evidence, not a proof. No composite number is known to pass the test with these bases, but a number of that size can only be proven prime with slower methods such as elliptic-curve primality proving, and this page doesn't run them.

Why won't it factor my 60-digit number?

Factoring a number whose two prime factors are both large takes more steps than a browser tab should spend. The page stops after a bounded number of tries and tells you what it found. The largest RSA number factored publicly, RSA-250, has 250 digits and took about 2,700 core-years of computing in 2020.

Can I link to a result?

Yes. The address updates to ?n= followed by the number you typed, which is harmless to share. Nothing else you do on the page leaves your browser.

Further reading

  1. Sorenson and Webster: Strong pseudoprimes to twelve prime bases (2017)
  2. Caldwell and Xiong: What is the smallest prime? (2012)
  3. Alford, Granville and Pomerance: There are infinitely many Carmichael numbers (1994)
  4. Brent: An improved Monte Carlo factorization algorithm (1980)
  5. Wikipedia: Miller-Rabin primality test