Math glossary

Prime Number Formulas: Tests, Patterns, and Examples

Find prime numbers using trial division, the 6k ± 1 filter, the sieve, and Wilson’s theorem. See which methods prove an answer.

TL;DR

Prime number formulas answer different questions. Trial division tests a single number, a sieve lists primes up to a limit, and Wilson’s theorem gives an exact test with factorials. The pattern 6k±16k\pm1 only filters candidates.

Key Points

  • A prime is a whole number greater than 1 with exactly two positive divisors.
  • To test one number by trial division, check prime divisors up to and including its square root.
  • All primes greater than 3 have the form 6k−16k-1 or 6k+16k+1, but those forms also include composites.
  • The sieve of Eratosthenes crosses out multiples to list every prime up to a chosen limit.
  • Wilson’s theorem is exact, while the prime number theorem gives an estimate.

What do prime number formulas tell you?

A prime number is an integer greater than 1 with exactly two positive factors, 1 and itself. Examples include 2, 3, 5, 7, 11, and 13. A composite has other positive factors. The number 1 has only one positive divisor, so it is neither prime nor composite. The prime and composite numbers lesson develops this definition with factor pairs.

The word formula can mean a candidate pattern, a proof that a given number is prime, or a way to get a list. Match the method to the question.

Your goal Method What the result can establish
Skip obvious composite candidates Remainders modulo 6 Primes above 3 must pass this filter.
Test one small whole number Trial division through n\sqrt n Gives a definite prime or composite result.
List every prime up to NN Sieve of Eratosthenes Returns the complete list up to the chosen limit.
Explore an exact factorial test Wilson’s theorem Gives a definite result, though the factorial grows quickly.
Estimate the number of primes in a range Prime number theorem Gives an asymptotic estimate, rather than an exact count.

Why does trial division stop at the square root?

If n=abn=ab is composite with a,b>1a,b>1, the smaller factor is at most n\sqrt n. If both factors were larger than n\sqrt n, their product would exceed nn. Also, a composite factor has a prime factor of its own. It is therefore enough to try prime divisors pp with p2≤np^2\le n. OpenStax’s factorization chapter develops the same use of prime factors inside composite numbers.

  1. Rule out n<2n<2 and handle n=2n=2 directly.
  2. Try small primes such as 2,3,5,7,…2,3,5,7,\ldots, stopping after the last prime at or below n\sqrt n.
  3. If any divides nn exactly, nn is composite. If none does, nn is prime.

The boundary is inclusive. For n=121n=121, test 11 because 112=12111^2=121. Stopping at factors strictly less than 11 gives the wrong classification.

Why do primes greater than 3 have the form 6k ± 1?

Every integer gives one of six remainders when divided by 6. Remainders 0, 2, and 4 give even numbers. A remainder of 3 gives a multiple of 3. For a prime greater than 3, only remainders 1 and 5 remain. A remainder of 5 can be written as one less than the next multiple of 6.

Remainder modulo 6 Result for an integer greater than 3
0, 2, or 4 Divisible by 2
3 Divisible by 3
1 or 5 Candidate for a prime test

For instance, 25=6(4)+125=6(4)+1 and 35=6(6)−135=6(6)-1. Both pass the remainder filter, yet 25=5225=5^2 and 35=5·735=5\cdot7. After testing 2 and 3, a trial-division procedure can use 6k−16k-1 and 6k+16k+1 as possible divisors, continuing only to the square root.

How does the sieve of Eratosthenes find a list of primes?

Write the integers from 2 through your limit NN. Starting at 2, cross out multiples of each smallest uncrossed number pp, beginning at p2p^2. Smaller multiples of pp have already been reached through a smaller prime factor. Stop crossing out when p2>Np^2>N. Virginia Tech’s OpenDSA lesson presents the sieve as a way to avoid repeated individual tests.

Number grid to 30 with prime numbers circled and composite numbers crossed out
Crossing out multiples of 2, 3, and 5 leaves all ten primes at or below 30.

For N=30N=30, process 2,3,52,3,5. They start crossing out at 4,9,254,9,25, respectively. The next uncrossed value is 7, and 72=49>307^2=49>30, so the sieve is finished.

Which formulas can generate primes or test them exactly?

Euler’s polynomial n2+n+41n^2+n+41 produces a prime for each integer n=0,1,…,39n=0,1,\ldots,39. It eventually fails. At n=40n=40, the first two terms sum to 40(41)40(41), giving 41(40+1)=41241(40+1)=41^2. Cal State San Marcos’s number theory handout uses prime-generating polynomials to investigate when such patterns break.

Wilson’s theorem says that an integer n>1n>1 is prime precisely when

(n−1)!≡−1(modn).(n-1)!\equiv-1\pmod n.

Here (n−1)!(n-1)! multiplies all positive integers from 1 through n−1n-1. The congruence says the factorial leaves remainder n−1n-1 upon division by nn. For a prime n>2n>2, every nonzero residue has a multiplicative inverse. Pair the residues with their inverses. Only 11 and −1-1 pair with themselves, since x2−1=(x−1)(x+1)x^2-1=(x-1)(x+1). The product is therefore −1-1 modulo nn. The case n=2n=2 works directly.

For a composite nn, choose a prime factor p<np<n. The factorial contains pp, so (n−1)!+1(n-1)!+1 leaves remainder 1 modulo pp. Because nn is divisible by pp, it cannot divide (n−1)!+1(n-1)!+1. Gordon College’s number theory text treats both directions of this residue test.

There are exact, more elaborate formulas for the nnth prime. For instance, James P. Jones discusses one in the Canadian Mathematical Bulletin. The existence of an exact formula does not guarantee a fast calculation. For a short list, a sieve is generally easier to carry out.

Can you estimate how many primes there are?

Let π(x)\pi(x) count primes at most xx. Here π(x)\pi(x) names a counting function and does not represent the circle constant. The prime number theorem, summarized in NIST’s Digital Library of Mathematical Functions, gives

π(x)~xlnx.\pi(x)\sim\frac{x}{\ln x}.

The symbol ~\sim means that the ratio of the two expressions approaches 1 as xx grows without bound. It does not make the two quantities equal at a particular xx. For example, at 100 the estimate is 100/ln100≈21.71100/\ln 100\approx21.71, while a sieve counts exactly 25 primes.

Examples of Prime Number Formulas

Example 1

Which of 33, 34, and 35 could still be prime after checking divisibility by 2 and 3?

  1. 3333 is divisible by 3 and 3434 is divisible by 2.
  2. 35=6(6)−135=6(6)-1 passes both quick divisibility checks.
  3. Testing 5 gives 35=5·735=5\cdot7. None of the three is prime.

Example 2

Is 29 prime?

  1. Since 52=255^2=25 and 62=366^2=36, the only prime divisors to test are 2, 3, and 5.
  2. The remainders of 29 upon division by 2,3,52,3,5 are 1,2,41,2,4, respectively.
  3. None is zero. Therefore 29 is prime.

Example 3

Is 121 prime?

  1. The pattern 121=6(20)+1121=6(20)+1 may make it look like a prime.
  2. Check divisors up to and including 121=11\sqrt{121}=11.
  3. Dividing by 11 gives exactly 11. Thus 121=112121=11^2 is composite.

Example 4

Use a sieve to find the primes up to 20.

  1. Keep 2 and cross out its multiples starting at 4.
  2. Keep 3 and cross out its multiples starting at 9. The next possible crossing prime is 5, but 25>2025>20.
  3. The survivor list is 2,3,5,7,11,13,17,192,3,5,7,11,13,17,19.

Example 5

Use n2+n+41n^2+n+41 at n=39n=39 and n=40n=40.

  1. At 39, the expression becomes 39(40)+41=160139(40)+41=1601. Checking primes up to 40 confirms that 1601 is prime.
  2. At 40, it becomes 40(41)+41=412=168140(41)+41=41^2=1681.
  3. One change in the input ends the initial streak. The polynomial gives an interesting pattern, but its output still needs a prime test.

Example 6

Check 5 and 9 with Wilson’s theorem.

  1. For 5, 4!=244!=24 has remainder 4 modulo 5, which equals −1-1 modulo 5. Therefore 5 is prime.
  2. For 9, 8!=403208!=40320 is divisible by 9, so its remainder is 0.
  3. The needed remainder for 9 would be 8. The test fails and 9 is composite.

What mistakes can misclassify a number?

Mistake 1: Treating every candidate as a prime

A number ending in 1, 3, 7, or 9 may still have a prime factor. For example, 77 avoids divisibility by 2 and 5 but equals 7·117\cdot11. Use patterns to cut down the number of checks, then confirm by testing divisors.

Mistake 2: Crossing out the prime you are using

When processing 3 in the sieve, cross out 9,12,15,…9,12,15,\ldots, leaving 3 itself on the grid. The starting point p2p^2 avoids crossing out pp or repeating the work done with smaller primes.

Try It Yourself

  1. Is 97 prime? Which divisors do you need to test?
  2. Is 49 a 6k±16k\pm1 candidate? Is it prime?
  3. What primes does a sieve leave from 21 through 30?

Answers and Reasoning

  1. Test 2,3,5,72,3,5,7. All leave nonzero remainders, so 97 is prime.
  2. 49=6(8)+149=6(8)+1 passes the filter, but 49=7249=7^2 is composite.
  3. The only primes in that interval are 23 and 29.

Questions About Prime Number Formulas

What is the smallest prime number?

It is 2. Its only positive divisors are 1 and 2. It is also the only even prime because all larger even integers are divisible by 2.

Is there a largest prime number?

There is no largest prime. If a finite list contained every prime, multiplying that list and adding 1 would give an integer divisible by none on the list. A prime factor of the new integer would have to be missing from the list.

Can you tell primality from the final digit?

For numbers greater than 5, a final digit of 0,2,4,5,6,0,2,4,5,6, or 88 proves the number is composite because it is divisible by 2 or 5. Final digits 1,3,7,1,3,7, and 99 leave the question open.

Conclusion

  • Use the remainder pattern to narrow candidates and trial division to verify a single small number.
  • Use a sieve to list all primes up to a limit.
  • Keep exact tests, prime-generating patterns, and counting estimates separate.

Read More

Research Behind This Article

  1. OpenStax, Prealgebra 2e: Prime Factorization and the Least Common Multiple. OpenStax
  2. Virginia Tech OpenDSA, Senior Algorithms: Finding Prime Numbers (Spring 2025). Virginia Tech
  3. California State University San Marcos, Math 422: Formulas for Primes. Cal State San Marcos
  4. Gordon College, Number Theory: In Context and Interactive, Wilson’s Theorem and Fermat’s Theorem (2023 edition). Gordon College
  5. James P. Jones, Formula for the nth Prime Number (1975). Canadian Mathematical Bulletin
  6. NIST, Digital Library of Mathematical Functions: Asymptotic Formulas for Primes. DLMF