Prime Number Calculator

Prime Number Calculator

#5 · Math and Statistics AI Skill PRO

Enter Calculation

Formula

isPrime(N)={1N>1and∄d∈[2,N]:Nmodd=00otherwise\text{isPrime}(\text{N}) = \begin{cases} 1 & \text{N} > 1 \ \text{and} \ \nexists \, d \in [2, \sqrt{\text{N}}] : \text{N} \bmod d = 0 \\[0.5em] 0 & \text{otherwise} \end{cases}
Usage stats

Advertisement

Results

Is 221 prime?
NO
This is a composite number
Number tested 221
Smallest divisor (> 1) 13
Prime factorization 221 = 13 × 17

Report an error on this page

What Is a Prime Number?

A prime number is a whole number greater than 1 that has exactly two distinct positive divisors: 1 and itself. Examples include 2, 3, 5, 7, 11, and 13. Numbers greater than 1 that are not prime are called composite — they can be written as a product of smaller whole numbers. This calculator tells you instantly whether any number you enter is prime, and if not, it shows you the smallest factor that divides it.

Which prime number tool do you need?

  • Prime number checker: whether a number is prime, with the previous and next prime.
  • Prime number calculator (this page): whether a number is prime and its smallest factor other than 1.
  • Nth prime: the nth prime, or a list of the first N primes.
  • Prime factorization: the prime factors of a number, with a factor tree.
  • Relatively prime: whether two numbers are coprime (no common factor other than 1).
Comparison of a prime number shown as a single row of dots versus a composite number arranged in a rectangular grid
A prime count cannot form a rectangle (other than \(1 \times n\)), while a composite count can.

How to Use the Calculator

Type any whole number (0 or larger) into the input box and submit. The result hero box shows YES if the number is prime or NO if it is composite. When the number is composite, the table reveals the smallest divisor greater than 1, which helps you start a full factorization.

Advertisement

The Formula Explained

To check primality efficiently, you only need to test divisors up to the square root of n. If n has a factor larger than its square root, it must also have a matching factor smaller than the square root, so checking that far is enough. Formally, \(n\) is prime when:

$$n \text{ is prime} \iff n > 1 \text{ and } n \bmod i \neq 0 \;\; \forall\, i \in [2, \sqrt{n}]$$

that is, \(n > 1\) and \(n \bmod i \neq 0\) for every integer \(i\) from 2 up to \(\lfloor\sqrt{n}\rfloor\). This reduces the work dramatically compared to testing every number below n.

Number line of divisors from 2 to n with a marker at the square root of n showing only the lower half needs checking
You only need to test divisors up to the square root of n.

Worked Example

Take \(n = 97\). Its square root is about 9.85, so we test divisors 2, 3, 5, 7, and 9. None of them divide 97 evenly (97 is odd, not divisible by 3, 5, or 7), so 97 is prime. Now take \(n = 91\). Testing 2, 3, 5, then 7 — we find $$91 \div 7 = 13$$ exactly, so 91 is composite with smallest divisor 7.

Advertisement

How to Check if a Number Is Prime by Hand

A prime is a whole number greater than 1 whose only positive divisors are 1 and itself. To test a whole number \(n\), you only need to look for divisors up to \(\lfloor\sqrt{n}\rfloor\). If \(n\) has a divisor larger than its square root, it must also have a matching one smaller than the square root.

  1. Rule out \(n < 2\). 0, 1 and negative numbers are not prime.
  2. Accept 2 and 3. Both are prime. 2 is the only even prime.
  3. Reject even numbers. Any even \(n > 2\) is divisible by 2, so it is composite.
  4. Compute \(\lfloor\sqrt{n}\rfloor\). Find the largest integer whose square is at most \(n\). For example, \(14^2 = 196 \le 221 < 225 = 15^2\), so \(\lfloor\sqrt{221}\rfloor = 14\).
  5. Test odd divisors, or just primes, from 3 up to \(\lfloor\sqrt{n}\rfloor\). Check whether \(n \bmod d = 0\) for each candidate \(d\). Use the divisibility shortcuts below to speed this up.
  6. Decide. If any divisor is found, \(n\) is composite, and the first one found is its smallest divisor greater than 1. If none divides \(n\), then \(n\) is prime.

Divisibility shortcuts

  • By 3: the sum of the digits is divisible by 3. Example: 51 gives 5 + 1 = 6, so 51 = 3 × 17.
  • By 5: the last digit is 0 or 5.
  • By 11: take the alternating sum of the digits, starting from the right or the left. If the result is 0 or a multiple of 11, the number is divisible by 11. Example: 121 gives 1 − 2 + 1 = 0, so 121 = 11 × 11.

Worked example 1: is 221 prime?

  1. 221 ≥ 2, it is odd, and it is not 2 or 3.
  2. \(\lfloor\sqrt{221}\rfloor = 14\), so test the primes 3, 5, 7, 11 and 13.
  3. 3: the digit sum is 2 + 2 + 1 = 5, which is not divisible by 3.
  4. 5: the last digit is 1, so no.
  5. 7: \(7 \times 31 = 217\) and \(221 - 217 = 4\), so there is a remainder and no.
  6. 11: the alternating sum is 1 − 2 + 2 = 1, so no.
  7. 13: \(13 \times 17 = 221\), so 13 divides 221.

221 is composite, and its smallest divisor is 13.

Worked example 2: is 97 prime?

  1. 97 is odd and greater than 3.
  2. \(9^2 = 81 \le 97 < 100 = 10^2\), so \(\lfloor\sqrt{97}\rfloor = 9\). Test 3, 5 and 7.
  3. 3: the digit sum is 9 + 7 = 16, which is not divisible by 3.
  4. 5: the last digit is 7, so no.
  5. 7: \(7 \times 13 = 91\) and \(97 - 91 = 6\), so there is a remainder and no.

No divisor up to 9 divides 97, so 97 is prime.

This hand method works well for small and medium numbers. For very large numbers, trial division becomes slow, and specialised primality tests are used instead.

Conventions and edge cases

Each case below was run through this calculator; the result column is what it actually returns.

CaseInputResult
Is 1 prime?1NO
Is 2 prime?2YES
Is 0 prime?0NO

Checked on 2026-11-08

FAQ

Is 1 a prime number? No. By definition a prime must be greater than 1, so 1 is neither prime nor composite.

Is 2 a prime number? Yes. 2 is the only even prime number; every other even number is divisible by 2.

Why only test up to the square root? Divisors come in pairs that multiply to n. If both were larger than \(\sqrt{n}\), their product would exceed n, which is impossible — so at least one divisor of any pair is \(\leq \sqrt{n}\).

Last updated:

Most popular in Math and Statistics

View all Math and Statistics calculators