wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are cowritten by multiple authors. To create this article, 62 people, some anonymous, worked to edit and improve it over time.
There are 8 references cited in this article, which can be found at the bottom of the page.
This article has been viewed 804,752 times.
Learn more...
Prime numbers are those divisible only by themselves and 1; all others are called composite numbers. While there are numerous ways to test for primality, there are trade offs. Perfect tests exist, but are extremely slow for large numbers, while much faster can give false results. Here are a few options to choose from depending on how large a number you are testing.
Steps
Part 1
Part 1 of 3:Prime Tests
Download Article
Note: In all formulas, n is the number being tested for primality.

1Trial division test. Divide n by each prime from 2 to floor().

2Fermat's Little Theorem. Warning: false positives are possible, even for all values of a.
 Choose an integer value for a such that 2 ≤ a ≤ n  1.
 If a^{n} (mod n) = a (mod n), then n is likely prime. If this is not true, n is not prime.
 Repeat with different values of a to increase confidence in primality
Advertisement 
3MillerRabin test. Warning: false positives are possible but rarely for multiple values of a.
 Find values for s and d such that .
 Choose an integer value for a such that 2 ≤ a ≤ n  1.
 If a^{d} = +1 (mod n) or 1 (mod n), then n is probably prime. Skip to test result. Otherwise, go to next step.
 Square your answer (). If this equals 1 (mod n), then n is probably prime. Skip to test result. Otherwise repeat ( etc.) until .
 If you ever square a number which is not (mod n) and end up with +1 (mod n), then n is not prime. If (mod n), then n is not prime.
 Test result: If n passes test, repeat with different values of a to increase confidence.
Advertisement
Part 2
Part 2 of 3:Understanding Prime Testing
Download Article

1Understand the trial division method. By the definition of primality, n is only prime if it cannot be divided evenly by integers 2 or greater. The formula given saves time by removing unnecessary tests (e.g. after testing 3 there is no need to test 9).
 Floor(x) rounds x to the closest integer ≤ x.

2Understand modular arithmetic. The "x mod y" operation (short for "modulo") means "divide x by y and find the remainder."^{[1] X Research source } In other words, in modular arithmetic, numbers "wrap around" back to zero upon reaching a certain value, called the modulus. A clock counts in modulo 12: it goes from 10 to 11 to 12, then wraps around back to 1.
 Many calculators have a mod button, but see the end of this section for how to solve this by hand for large numbers.

3Know the pitfalls of Fermat's Little Theorem. All numbers that fail this test are composite (nonprime), but unfortunately numbers that pass this test are only likely primes. If you want to be sure of avoiding false positives, look for n on a list of "Carmichael numbers" (which pass this test every time) and "Fermat pseudoprimes" (which pass this test only for some values of a).^{[2] X Research source }

4Use the MillerRabin test whenever practical. Although tedious to perform by hand, this test is commonly used in software. This can be performed at a practical speed and gives fewer false positives than Fermat's method.^{[3] X Research source } A composite number never gives a false positive for more than ¼ of the values of a.^{[4] X Research source } If you choose several values of a at random and they all pass this test, you can be fairly confident that n is prime.

5Perform modular arithmetic for large numbers. If you do not have access to a calculator with a mod function, or if your calculator can't display numbers that high, use properties of exponents and modular arithmetic to make the process easier.^{[5] X Research source } Here's an example for mod 50:
 Rewrite the expression with more manageable exponents: mod 50. (You may need to break it down further if calculating by hand).
 mod 50 = mod 50 mod 50) mod 50. (This is a property of modular multiplication.)
 mod 50 = 43.
 mod 50 mod 50) mod 50 = mod 50
 mod 50
Advertisement
Part 3
Part 3 of 3:Chinese Remainder Theorem Test
Download Article

1Choose two numbers. One of the numbers is not prime and the second number is the number that needs to be tested for primality.
 "Prime1" = 35
 Prime2 = 97

2Choose two datapoints that are greater than zero and less than prime1 and prime2 respectfully. They can't equal each other.
 Data1 = 1
 Data2 = 2

3Calculate MMI (Mathematical Multiplicative Inverse) for Prime1 and Prime2
 Calculate MMI
 MMI1 = Prime2 ^ 1 Mod Prime1
 MMI2 = Prime1 ^ 1 Mod Prime2
 For Prime Numbers only (it will give a number for nonprime numbers but it won't be its MMI):
 MMI1 = (Prime2 ^ (Prime12)) % Prime1
 MMI2 = (Prime1 ^ (Prime22)) % Prime2
 e.g
 MMI1 = (97 ^ 33) % 35
 MMI2 = (35 ^ 95) % 97
 Calculate MMI

4Create a binary table for each MMI up to Log2 of the Modulus
 For MMI1
 F(1) = Prime2 % Prime1 = 97 % 35 = 27
 F(2) = F(1) * F(1) % Prime1 = 27 * 27 % 35 = 29
 F(4) = F(2) * F(2) % Prime1 = 29 * 29 % 35 = 1
 F(8) = F(4) * F(4) % Prime1 = 1 * 1 % 35 = 1
 F(16) =F(8) * F(8) % Prime1 = 1 * 1 % 35 = 1
 F(32) =F(16) * F(16) % Prime1 = 1 * 1 % 35 = 1
 Calculate the binary of Prime1  2
 35 2 = 33 (10001) base 2
 MMI1 = F(33) = F(32) * F(1) mod 35
 MMI1 = F(33) = 1 * 27 Mod 35
 MMI1 = 27
 For MMI2
 F(1) = Prime1 % Prime2 = 35 % 97 = 35
 F(2) = F(1) * F(1) % Prime2 = 35 * 35 mod 97 = 61
 F(4) = F(2) * F(2) % Prime2 = 61 * 61 mod 97 = 35
 F(8) = F(4) * F(4) % Prime2 = 35 * 35 mod 97 = 61
 F(16) = F(8) * F(8) % Prime2 = 61 * 61 mod 97 = 35
 F(32) = F(16) * F(16) % Prime2 = 35 * 35 mod 97 = 61
 F(64) = F(32) * F(32) % Prime2 = 61 * 61 mod 97 = 35
 F(128) = F(64) * F(64) % Prime2 = 35 * 35 mod 97 = 61
 Calculate the binary of Prime2  2
 97  2 = 95 = (1011111) base 2
 MMI2 = (((((F(64) * F(16) % 97) * F(8) % 97) * F(4) % 97) * F(2) % 97) * F(1) % 97)
 MMI2 = (((((35 * 35) %97) * 61) % 97) * 35 % 97) * 61 % 97) * 35 % 97)
 MMI2 = 61
 For MMI1

5Calculate (Data1 * Prime2 * MMI1 + Data2 * Prime1 * MMI2) % (Prime1 * Prime2)
 Answer = (1 * 97 * 27 + 2 * 35 * 61) % (97 * 35)
 Answer = (2619 + 4270) % 3395
 Answer = 99

6Verify that "Prime1" is not Prime
 Calculate (Answer  Data1) % Prime1
 99 1 % 35 = 28
 Since 28 is greater than 0, 35 is not prime

7Check if Prime2 is Prime
 Calculate (Answer  Data2) % Prime2
 99  2 % 97 = 0
 Since 0 equals 0, 97 is potentially prime

8Repeat steps 1 through 7 at least two more times.
 If step 7 is 0:
 Use a different "prime1" where prime1 is a nonprime
 Use a different prime 1 where prime 1 is an actual prime. In this case, steps 6 and 7 should equal 0.
 Use different data points for data1 and data2.
 If step 7 is 0 every time, there is an extremely high probability that prime2 is prime.
 Steps 1 though 7 are known to fail in certain cases when the first number is a nonprime number and the second prime is a factor of the nonprime number "prime1". It works in all scenarios where both numbers are prime.
 The reason why steps 1 though 7 are repeated is because there are a few scenarios where, even if prime1 is not prime and prime2 is not prime, step 7 still works out to be zero, for one or both the numbers. These circumstances are rare. By changing prime1 to a different nonprime number, if prime2 is not prime, prime2 will rapidly not equal zero in step 7. Except for the instance where "prime1" is a factor of prime2, prime numbers will always equal zero in step 7.
Advertisement  If step 7 is 0:
Community Q&A

QuestionWhy do I use odd numbers in testing for primes?Community AnswerBecause, other than 2, there can be no even prime numbers (they would be divisible by 2).

QuestionAre 57, 87, 89, and 91 prime numbers?Gaurav WasnikCommunity Answer57 is divisible by 3 and 19 in addition to 1 and itself so it is not prime. 87 is also divisible by 3 so it is not prime. 89 is a prime number because it is only divisible by 1 and itself. 91 is divisible by 7 so it is not prime.

QuestionIs it true that since 43 x 1069 = 45,967, it can't be a prime number?Community AnswerYes. Prime numbers cannot be the product of any two numbers other than 1 and the number itself. So 45,967 is not prime.

QuestionDo prime numbers have a pattern?Community AnswerUnfortunately, there isn't any obvious or useful patterns among prime numbers.

QuestionAre there any ataglance quick way to have a good guess at whether a number is prime or not?DonaganTop AnswererNo, not at a glance. The closest you can come is knowing that a prime number is always odd but never ends in a 5. A prime number will end in a 1, 3, 7, or 9, but not all such numbers are prime.
Tips
 The 168 prime numbers under 1000 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 ^{[6] X Research source }Thanks!
 While trial division is slower than more sophisticated methods for large numbers, it is still quite efficient for small numbers. Even for primality testing of large numbers, it is not uncommon to first check for small factors before switching to a more advanced method when no small factors are found.Thanks!
Things You'll Need
 Working out tools, such as paper and pen or a computer
References
 ↑ http://betterexplained.com/articles/funwithmodulararithmetic/
 ↑ http://mathworld.wolfram.com/FermatsLittleTheorem.html
 ↑ http://www.cs.cornell.edu/courses/cs4820/2010sp/handouts/MillerRabin.pdf
 ↑ https://books.google.com/books?id=QbVtCQAAQBAJ&dq=millerrabin+1/4+false+positives
 ↑ https://www.khanacademy.org/computing/computerscience/cryptography/modarithmetic/a/modularexponentiation
 ↑ Online Encyclopedia of Integer Sequences, A000040
 Topcoder.com  sample source code and documentation for methods discussed here
 Online Prime Number Checker  check numbers with up to 5000 digits
About This Article
To check if a number is prime, divide it by every prime number starting with 2, and ending when the square of the prime number is greater than the number you’re checking against. If it is not evenly divided by any whole number other than 1 or itself, the number is prime. If you want to learn how to do modular arithmetic to test large numbers, keep reading the article!