[Home](https://learningfolio.de5.net/) / [Math](https://learningfolio.de5.net/math/) / [Math glossary](https://learningfolio.de5.net/math/glossary/) / Prime Number Formulas: Tests, Patterns, and Examples

[Math glossary](https://learningfolio.de5.net/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.

Written by **Learningfolio Team**

We’re subject-matter experts in math, science, parent support, and education, with 100 years of combined experience. We believe in learning, experimenting, and growing, and we review one another’s work.

## On this page

* [TL;DR](https://learningfolio.de5.net/math/prime-number-formulas/#tldr)
* [Key Points](https://learningfolio.de5.net/math/prime-number-formulas/#key-points)
* [What do prime number formulas tell you?](https://learningfolio.de5.net/math/prime-number-formulas/#what-do-prime-number-formulas-tell-you)
* [Why does trial division stop at the square root?](https://learningfolio.de5.net/math/prime-number-formulas/#why-does-trial-division-stop-at-the-square-root)
* [Why do primes greater than 3 have the form 6k ± 1?](https://learningfolio.de5.net/math/prime-number-formulas/#why-do-primes-greater-than-3-have-the-form-6k-1)
* [How does the sieve of Eratosthenes find a list of primes?](https://learningfolio.de5.net/math/prime-number-formulas/#how-does-the-sieve-of-eratosthenes-find-a-list-of-primes)
* [Which formulas can generate primes or test them exactly?](https://learningfolio.de5.net/math/prime-number-formulas/#which-formulas-can-generate-primes-or-test-them-exactly)
* [Can you estimate how many primes there are?](https://learningfolio.de5.net/math/prime-number-formulas/#can-you-estimate-how-many-primes-there-are)
* [Examples of Prime Number Formulas](https://learningfolio.de5.net/math/prime-number-formulas/#examples-of-prime-number-formulas)
* [What mistakes can misclassify a number?](https://learningfolio.de5.net/math/prime-number-formulas/#what-mistakes-can-misclassify-a-number)
* [Try It Yourself](https://learningfolio.de5.net/math/prime-number-formulas/#try-it-yourself)
* [Questions About Prime Number Formulas](https://learningfolio.de5.net/math/prime-number-formulas/#questions-about-prime-number-formulas)
* [Conclusion](https://learningfolio.de5.net/math/prime-number-formulas/#conclusion)
* [Read More](https://learningfolio.de5.net/math/prime-number-formulas/#read-more)
* [Research Behind This Article](https://learningfolio.de5.net/math/prime-number-formulas/#research-behind-this-article)

## 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\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-1$ or $6k+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](https://learningfolio.de5.net/math/prime-and-composite-numbers/) 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 $\sqrt n$ | Gives a definite prime or composite result. |
| List every prime up to $N$ | 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=ab$ is composite with $a,b>1$, the smaller factor is at most $\sqrt n$. If both factors were larger than $\sqrt n$, their product would exceed $n$. Also, a composite factor has a prime factor of its own. It is therefore enough to try **prime divisors** $p$ with $p^2\le n$. OpenStax’s factorization chapter develops the same use of prime factors inside composite numbers.

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

The boundary is inclusive. For $n=121$, test 11 because $11^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)+1$ and $35=6(6)-1$. Both pass the remainder filter, yet $25=5^2$ and $35=5\cdot7$. After testing 2 and 3, a trial-division procedure can use $6k-1$ and $6k+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 $N$. Starting at 2, cross out multiples of each smallest uncrossed number $p$, beginning at $p^2$. Smaller multiples of $p$ have already been reached through a smaller prime factor. Stop crossing out when $p^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](https://learningfolio.de5.net/assets/versioned/prime-number-formulas-sieve.bf305d4a332b.webp)


Crossing out multiples of 2, 3, and 5 leaves all ten primes at or below 30.

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

## Which formulas can generate primes or test them exactly?

**Euler’s polynomial** $n^2+n+41$ produces a prime for each integer $n=0,1,\ldots,39$. It eventually fails. At $n=40$, the first two terms sum to $40(41)$, giving $41(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>1$ is prime precisely when

$$
(n-1)!\equiv-1\pmod n.
$$

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

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

There are exact, more elaborate formulas for the $n$th 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 $\pi(x)$ count primes at most $x$. Here $\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

$$
\pi(x)\sim\frac{x}{\ln x}.
$$

The symbol $\sim$ means that the ratio of the two expressions approaches 1 as $x$ grows without bound. It does not make the two quantities equal at a particular $x$. For example, at 100 the estimate is $100/\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. $33$ is divisible by 3 and $34$ is divisible by 2.
2. $35=6(6)-1$ passes both quick divisibility checks.
3. Testing 5 gives $35=5\cdot7$. None of the three is prime.

### Example 2

Is 29 prime?

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

### Example 3

Is 121 prime?

1. The pattern $121=6(20)+1$ may make it look like a prime.
2. Check divisors up to and including $\sqrt{121}=11$.
3. Dividing by 11 gives exactly 11. Thus $121=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>20$.
3. The survivor list is $2,3,5,7,11,13,17,19$.

### Example 5

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

1. At 39, the expression becomes $39(40)+41=1601$. Checking primes up to 40 confirms that 1601 is prime.
2. At 40, it becomes $40(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!=24$ has remainder 4 modulo 5, which equals $-1$ modulo 5. Therefore 5 is prime.
2. For 9, $8!=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\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,\ldots$, leaving 3 itself on the grid. The starting point $p^2$ avoids crossing out $p$ 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\pm1$ candidate? Is it prime?
3. What primes does a sieve leave from 21 through 30?

### Answers and Reasoning

1. Test $2,3,5,7$. All leave nonzero remainders, so 97 is prime.
2. $49=6(8)+1$ passes the filter, but $49=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,$ or $8$ proves the number is composite because it is divisible by 2 or 5. Final digits $1,3,7,$ and $9$ 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

* [Why one is neither prime nor composite](https://learningfolio.de5.net/math/one-neither-prime-nor-composite/)

## Research Behind This Article

1. OpenStax, *Prealgebra 2e: Prime Factorization and the Least Common Multiple*. [OpenStax](https://openstax.org/books/prealgebra-2e/pages/2-5-prime-factorization-and-the-least-common-multiple)
2. Virginia Tech OpenDSA, *Senior Algorithms: Finding Prime Numbers* (Spring 2025). [Virginia Tech](https://opendsa-server.cs.vt.edu/ODSA/Books/vt/cs4104-80dcbe0b-7910-4c9a-8836-8d098d893019/spring-2025/S25/html/Primes.html)
3. California State University San Marcos, *Math 422: Formulas for Primes*. [Cal State San Marcos](https://public.csusm.edu/aitken_html/m422/Handout4.pdf)
4. Gordon College, *Number Theory: In Context and Interactive, Wilson’s Theorem and Fermat’s Theorem* (2023 edition). [Gordon College](https://www.math.gordon.edu/ntic/ntic2023/section-wilson-flt.html)
5. James P. Jones, *Formula for the nth Prime Number* (1975). [Canadian Mathematical Bulletin](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/BD8E80119B456117811C431BAB9EFCCD/S0008439500068521a.pdf/formula_for_the_nth_prime_number.pdf)
6. NIST, *Digital Library of Mathematical Functions: Asymptotic Formulas for Primes*. [DLMF](https://dlmf.nist.gov/27.12)
