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 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 or , 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 | Gives a definite prime or composite result. |
| List every prime up to | 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 is composite with , the smaller factor is at most . If both factors were larger than , their product would exceed . Also, a composite factor has a prime factor of its own. It is therefore enough to try prime divisors with . OpenStax’s factorization chapter develops the same use of prime factors inside composite numbers.
- Rule out and handle directly.
- Try small primes such as , stopping after the last prime at or below .
- If any divides exactly, is composite. If none does, is prime.
The boundary is inclusive. For , test 11 because . 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, and . Both pass the remainder filter, yet and . After testing 2 and 3, a trial-division procedure can use and 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 . Starting at 2, cross out multiples of each smallest uncrossed number , beginning at . Smaller multiples of have already been reached through a smaller prime factor. Stop crossing out when . Virginia Tech’s OpenDSA lesson presents the sieve as a way to avoid repeated individual tests.
For , process . They start crossing out at , respectively. The next uncrossed value is 7, and , so the sieve is finished.
Which formulas can generate primes or test them exactly?
Euler’s polynomial produces a prime for each integer . It eventually fails. At , the first two terms sum to , giving . 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 is prime precisely when
Here multiplies all positive integers from 1 through . The congruence says the factorial leaves remainder upon division by . For a prime , every nonzero residue has a multiplicative inverse. Pair the residues with their inverses. Only and pair with themselves, since . The product is therefore modulo . The case works directly.
For a composite , choose a prime factor . The factorial contains , so leaves remainder 1 modulo . Because is divisible by , it cannot divide . Gordon College’s number theory text treats both directions of this residue test.
There are exact, more elaborate formulas for the 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 count primes at most . Here 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
The symbol means that the ratio of the two expressions approaches 1 as grows without bound. It does not make the two quantities equal at a particular . For example, at 100 the estimate is , 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?
- is divisible by 3 and is divisible by 2.
- passes both quick divisibility checks.
- Testing 5 gives . None of the three is prime.
Example 2
Is 29 prime?
- Since and , the only prime divisors to test are 2, 3, and 5.
- The remainders of 29 upon division by are , respectively.
- None is zero. Therefore 29 is prime.
Example 3
Is 121 prime?
- The pattern may make it look like a prime.
- Check divisors up to and including .
- Dividing by 11 gives exactly 11. Thus is composite.
Example 4
Use a sieve to find the primes up to 20.
- Keep 2 and cross out its multiples starting at 4.
- Keep 3 and cross out its multiples starting at 9. The next possible crossing prime is 5, but .
- The survivor list is .
Example 5
Use at and .
- At 39, the expression becomes . Checking primes up to 40 confirms that 1601 is prime.
- At 40, it becomes .
- 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.
- For 5, has remainder 4 modulo 5, which equals modulo 5. Therefore 5 is prime.
- For 9, is divisible by 9, so its remainder is 0.
- 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 . 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 , leaving 3 itself on the grid. The starting point avoids crossing out or repeating the work done with smaller primes.
Try It Yourself
- Is 97 prime? Which divisors do you need to test?
- Is 49 a candidate? Is it prime?
- What primes does a sieve leave from 21 through 30?
Answers and Reasoning
- Test . All leave nonzero remainders, so 97 is prime.
- passes the filter, but is composite.
- 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 or proves the number is composite because it is divisible by 2 or 5. Final digits and 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
- OpenStax, Prealgebra 2e: Prime Factorization and the Least Common Multiple. OpenStax
- Virginia Tech OpenDSA, Senior Algorithms: Finding Prime Numbers (Spring 2025). Virginia Tech
- California State University San Marcos, Math 422: Formulas for Primes. Cal State San Marcos
- Gordon College, Number Theory: In Context and Interactive, Wilson’s Theorem and Fermat’s Theorem (2023 edition). Gordon College
- James P. Jones, Formula for the nth Prime Number (1975). Canadian Mathematical Bulletin
- NIST, Digital Library of Mathematical Functions: Asymptotic Formulas for Primes. DLMF