Arithmetic

How to Check if a Number Is Prime: Prime Test by Division

To check if a number is prime, test every divisor from 2 up to its square root for a remainder of 0. Learn the trial division method, the square root shortcut, and a code test.

By Remainder Calculator Team

Checking whether a number is prime means testing whether it has any divisor other than 1 and itself. To check if a number is prime, divide it by every integer from 2 up to its square root; if none of those divisions leaves a remainder of 0, the number is prime. Testing 97 against 2, 3, 5 and 7 leaves a remainder every time, so 97 is prime. The Remainder Calculator confirms each division, and this guide explains the prime test by division step by step.

The prime test by division delivers 3 advantages: it needs only remainders and no advanced math, it stops early at the square root, and it names a factor the moment a test fails. Prime checking underlies cryptography, hashing, fraction reduction and number theory.

Every prime test has 3 parts: the candidate number, the list of trial divisors, and the remainder each division leaves. This guide covers what a prime number is, the trial division method, why the square root is the stopping point, and how to write the test in code.

What Is a Prime Number?

A prime number is a whole number greater than 1 that has exactly 2 factors: 1 and itself. A number with any other factor is composite.

The number 7 is prime, because only 1 and 7 divide it evenly. The number 8 is composite, because 2 and 4 also divide it. Two boundary cases matter: 1 is not prime, since it has only 1 factor, and 2 is the only even prime, since every other even number has 2 as a factor. A factor is any divisor that leaves a remainder of 0, so a remainder of zero is the exact signal that a candidate is composite.

How to Check if a Number Is Prime

The prime test by division checks the candidate against each integer from 2 upward, looking for a remainder of 0. A single zero remainder proves the number composite; reaching the square root with no zero remainder proves it prime.

Check whether 91 is prime:

  1. Divide by 2. 91 is odd, so 91 % 2 is 1. Not a factor.
  2. Divide by 3. The digits sum to 10, so 91 % 3 is 1. Not a factor.
  3. Divide by 5. 91 does not end in 0 or 5, so 91 % 5 is 1. Not a factor.
  4. Divide by 7. 91 % 7 is 0, since 7 × 13 = 91. A factor exists.

The test stops at step 4: 91 has a factor of 7, so 91 is composite, not prime. The division that reaches a remainder of 0 also names the factor, which is why the standard method for finding a remainder is the core of every prime test, and each trial division can be carried out with the full long division method.

Why Test Only up to the Square Root

The prime test stops at the square root of the candidate, because factors come in pairs that straddle it. If a number has a factor larger than its square root, the paired factor must be smaller.

For 97, the square root is about 9.8, so the test needs only the divisors 2, 3, 5 and 7. Any factor of 97 above 9.8 would pair with a factor below 9.8, and the smaller one would already have been found. Testing beyond the square root repeats work that the earlier divisors have covered, so stopping there cuts the test dramatically for large numbers.

Check whether 97 is prime:

  1. 97 % 2 = 1, since 97 is odd.
  2. 97 % 3 = 1, since the digits sum to 16.
  3. 97 % 5 = 2, since 97 ends in 7.
  4. 97 % 7 = 6, since 7 × 13 = 91.

All 4 divisions leave a remainder, and 7 is the largest integer below √97, so 97 is prime.

Speed the Test up With Divisibility Rules

Divisibility rules remove most trial divisions before any arithmetic. Each rule answers whether a small divisor gives a remainder of 0 in a single glance.

Three rules eliminate the most common factors:

  • The rule for 2 rejects every even candidate except 2 itself.
  • The rule for 3 sums the digits, so a digit sum divisible by 3 marks a composite.
  • The rule for 5 rejects any candidate ending in 0 or 5.

Applying these first means a prime test often reduces to checking 7, 11 and 13. The full set of tests sits in divisibility rules 1 to 20, and the same last-digit and digit-sum checks appear among the shortcuts for ruling out divisors quickly.

How to Write a Prime Test in Code

A prime test in code loops from 2 to the square root of n and returns false if any n % i == 0. The modulo operator supplies the remainder, and a single zero ends the test.

function isPrime(n) {
  if (n < 2) return false;
  for (let i = 2; i * i <= n; i++) {
    if (n % i === 0) return false;
  }
  return true;
}

The condition i * i <= n replaces a square root call and stops the loop at the right point. The check n % i === 0 returns true when i divides n exactly, which is writing a primality test with modulo. The same loop behaves identically for positive inputs across programming languages, since a zero remainder carries no sign, unlike the remainder of a negative number.

Worked Examples

NumberDivisors testedFirst factor foundPrime?
2none neededYes
512, 33 (51 = 3 × 17)No
912, 3, 5, 77 (91 = 7 × 13)No
972, 3, 5, 7noneYes
1432, 3, 5, 7, 1111 (143 = 11 × 13)No
2112, 3, 5, 7, 11, 13noneYes

Common Mistakes

Four errors cause most wrong prime results:

  • Calling 1 a prime, when a prime needs exactly 2 factors and 1 has only one.
  • Testing every divisor up to n instead of stopping at the square root.
  • Skipping the divisor 2, which misses every even composite.
  • Assuming a large odd number is prime without testing 7, 11 and 13.

Numbers like 91 and 143 look prime because they are odd and pass the rules for 2, 3 and 5, yet both factor cleanly. Reaching the square root is the only proof that no factor exists.

Frequently Asked Questions

How do you check if a number is prime?

To check if a number is prime, divide it by every integer from 2 up to its square root. If no division leaves a remainder of 0, the number is prime; a single zero remainder makes it composite.

Why only test up to the square root?

Testing stops at the square root because factors come in pairs that straddle it. Any factor above the square root pairs with a smaller factor that the earlier divisions already checked.

Is 1 a prime number?

No, 1 is not a prime number. A prime has exactly 2 distinct factors, 1 and itself, but 1 has only a single factor.

What is the fastest way to rule out a divisor?

The fastest way to rule out a divisor is a divisibility rule. The rules for 2, 3 and 5 reject most candidates by reading the last digit or the digit sum, before any division is needed.

Conclusion

Checking if a number is prime means dividing it by every integer from 2 to its square root and watching for a remainder of 0. A single zero remainder names a factor and proves the number composite, while reaching the square root with no zero remainder proves it prime.

The 3 advantages of the trial division method, no advanced math, an early stop at the square root, and a named factor on failure, make it the standard hand and code test. Divisibility rules speed it up by rejecting small divisors first. Enter any candidate and divisor into the Remainder Calculator to test each division for a remainder of 0.

← Back to Blog