Math calculators

Prime Number Calculator

Updated Jul 19, 2026 By Jehan Wadia
Rate Formulas

A prime number is a positive integer greater than 1 that has exactly two factors: 1 and itself.

6 / 1000

Try an example

Step-by-Step Solution

Prime Distribution Near This Number

Your number Prime Not prime

Introduction

A prime number is a whole number greater than 1 that can only be divided evenly by 1 and itself. For example, 7 is prime because no other number divides into it evenly. The number 6 is not prime because 2 × 3 = 6.

This prime number calculator lets you check if any number is prime, find the next prime after a number, or find the previous prime before a number. It works with small numbers and very large numbers — up to 1,000 digits long. You can also type math expressions like 2^31-1 or 5!+1, and the calculator will solve them first, then test the result.

For numbers with 15 digits or fewer, the tool tests every possible factor to give you an exact answer. For larger numbers, it uses a fast method called the Miller–Rabin test to determine if a number is almost certainly prime. Every result comes with a step-by-step breakdown so you can see how the answer was found.

How to Use Our Prime Number Calculator

Type a number or math expression and pick what you want to find out. The calculator will tell you if your number is prime, show you the work step by step, and display a chart of nearby primes.

Enter a number or expression: Type a whole number like 97 or a math expression like 2^31-1 into the input box. You can use addition (+), subtraction (-), multiplication (*), division (/), exponents (^), factorials (!), and parentheses. The number can be up to 1,000 digits long.

Action: Use the dropdown menu to choose what you want to do. Pick Check — Is it prime? to test if your number is prime or composite. Pick Find Next Prime to get the first prime number above yours. Pick Find Previous Prime to get the first prime number below yours.

Calculate: Click the Check Number button to run the calculator. Your result will appear below with a clear label, a full list of factors when available, the nearest primes, and a step-by-step breakdown of how the answer was found.

What Is a Prime Number?

A prime number is a whole number greater than 1 that can only be divided evenly by 1 and itself. For example, 7 is prime because no other number divides into it without a remainder. The number 6 is not prime because 2 × 3 = 6, which means it has extra factors besides 1 and 6.

The First Prime Numbers

The smallest prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29. Notice that 2 is the only even prime number. Every other even number can be divided by 2, so none of them can be prime. The number 1 is not prime and not composite — it is in a class by itself.

Why Prime Numbers Matter

Every whole number greater than 1 is either prime or can be broken down into a product of primes. For instance, 12 = 2 × 2 × 3. This idea is called the Fundamental Theorem of Arithmetic, and it means prime numbers are the building blocks of all other numbers. They play a key role in computer security, online banking, and encrypted messaging because it is very hard to factor large numbers back into their prime parts.

How to Tell if a Number Is Prime

For small numbers, you can test every whole number from 2 up to the square root of the number. If none of them divide evenly, the number is prime. This method is called trial division. You can use a modulo calculator to check whether one number divides evenly into another. For very large numbers with many digits, trial division takes too long. Instead, mathematicians use fast tests like the Miller–Rabin test, which can say a number is "almost certainly prime" in a fraction of a second. A big number calculator can help you work with integers that have hundreds or even thousands of digits. These tests are not 100% guaranteed, but they are extremely reliable.

Do Prime Numbers Ever End?

No. There are infinitely many prime numbers. The ancient Greek mathematician Euclid proved this over 2,000 years ago. No matter how far you count, there will always be another prime waiting ahead. As numbers get bigger, primes become less common but never stop appearing entirely.


Formulas used

Trial Division Primality Test
n \text{ is prime} \iff n \bmod k \neq 0 \;\; \forall \; 2 \le k \le \lfloor\sqrt{n}\rfloor
Miller–Rabin Decomposition
n - 1 = 2^{r} \cdot d, \quad d \text{ odd}
Miller–Rabin Witness Test
a^{d} \equiv 1 \pmod{n} \;\text{ or }\; a^{2^{i}\cdot d} \equiv -1 \pmod{n} \;\text{ for some } 0 \le i < r
Modular Exponentiation (Binary Method)
b^{e} \bmod m : \; r \leftarrow 1,\; \text{while } e > 0: \; \text{if } e \bmod 2 = 1 \text{ then } r \leftarrow r \cdot b \bmod m;\; b \leftarrow b^{2} \bmod m;\; e \leftarrow \lfloor \frac{e}{2} \rfloor
Factorial
n! = \prod_{k=1}^{n} k
Stirling's Approximation (Factorial Digit Estimate)
\text{digits}(n!) \approx \lfloor n\log_{10} n - n\log_{10} e + \frac{1}{2}\log_{10}(2\pi n) \rfloor + 1

Frequently asked questions

Is 1 a prime number?

No. The number 1 is not a prime number. By definition, a prime must be greater than 1 and have exactly two factors. The number 1 only has one factor — itself. It is also not composite. Mathematicians put 1 in its own special category.

Is 0 a prime number?

No. Zero is not a prime number. Prime numbers must be whole numbers greater than 1. Zero can be divided by every number, so it does not meet the definition of prime. If you enter 0 into the calculator, it will let you know the input is not valid for a primality check.

What is a composite number?

A composite number is a whole number greater than 1 that has more than two factors. For example, 12 is composite because it can be divided by 1, 2, 3, 4, 6, and 12. Every whole number greater than 1 is either prime or composite. When you check a number with this calculator and it is not prime, the result will say "composite" and show you its factors.

What does probably prime mean?

When a number has more than 15 digits, the calculator uses a fast test called Miller–Rabin instead of checking every possible factor. This test is extremely reliable but not 100% guaranteed. So the result says "probably prime" instead of just "prime." The chance of a mistake is incredibly small — far less than 1 in a trillion.

Can I check negative numbers for primality?

No. Prime numbers are defined as positive whole numbers greater than 1. Negative numbers cannot be prime. If you enter a negative number and choose "Check," the calculator will show an error. You can, however, use the "Find Next Prime" or "Find Previous Prime" modes with negative inputs, since the calculator will search from that starting point.

Can I use decimals or fractions?

No. Primality only applies to whole numbers. If your expression results in a decimal, the calculator will tell you it cannot check it. Make sure your input simplifies to a whole number. For example, 10/3 gives a decimal and will not work, but 10/2 gives 5, which is a valid input.

What is the largest number this calculator can check?

This calculator can handle numbers up to 1,000 digits long. That is far larger than most people will ever need. Numbers with 15 digits or fewer get an exact answer using trial division. Numbers with 16 to 1,000 digits get a highly reliable probable answer using the Miller–Rabin test.

What is the largest known prime number?

The largest known prime number as of recent records is a Mersenne prime, which is a prime that takes the form 2p − 1. These massive primes have millions of digits. New record primes are found by the Great Internet Mersenne Prime Search (GIMPS) project using thousands of computers working together.

What are twin primes?

Twin primes are pairs of prime numbers that are exactly 2 apart. Examples include 3 and 5, 11 and 13, and 41 and 43. You can use this calculator to spot twin primes by checking a number and looking at the nearest primes shown in the result. Whether there are infinitely many twin primes is still an unsolved problem in math.

Why is 2 the only even prime number?

The number 2 is prime because its only factors are 1 and 2. Every other even number can be divided by 2, which gives it an extra factor. That extra factor means it cannot be prime. So 2 is the one and only even prime, and every other prime number is odd.

What does the chart below the result show?

The chart shows a small range of numbers around your input and marks which ones are prime and which are not. Green bars are prime numbers, gray bars are not prime, and the dark purple bar is the number you entered. This gives you a quick visual picture of how primes are spread out near your number.

How does the Find Next Prime feature work?

When you choose Find Next Prime, the calculator starts at your number and counts up, testing each number until it finds one that is prime. For small numbers, it checks every candidate exactly. For large numbers, it uses the Miller–Rabin test. The result is shown along with a step-by-step explanation.

How does the Find Previous Prime feature work?

When you choose Find Previous Prime, the calculator starts at your number and counts down, testing each number until it finds a prime. If your number is 2 or less, no previous prime exists and the calculator will tell you. For large numbers, the result is a probable prime found by the Miller–Rabin test.

What math expressions can I type into the calculator?

You can use + for addition, - for subtraction, * for multiplication, / for division, ^ for exponents, ! for factorials, and parentheses ( ) for grouping. For example, 2^31-1 means 2 raised to the 31st power minus 1. The calculator evaluates your expression first, then tests the result.

What is a Mersenne prime?

A Mersenne prime is a prime number that can be written as 2p − 1, where p is also a prime. For example, 25 − 1 = 31, and 31 is prime. Not every number of this form is prime, but the ones that are tend to be the largest primes ever discovered. You can test Mersenne numbers in this calculator by typing expressions like 2^61-1.