Skip to main content
Skip to calculator
Advertisement

Last updated: August 5, 2026

Prime Number Calculator

Quick Answer

The Prime Number Calculator tests whether a whole number is prime using trial division up to its square root. It reports the prime/composite classification, the smallest factor for composite numbers, and the nearest prime neighbors, giving a complete picture of where the number sits in the sequence of primes.

A number is prime if the only whole numbers that divide it evenly are one and itself.

Key Takeaways

  • A prime number has exactly two positive divisors: 1 and itself.
  • Trial division only needs to test candidates up to the square root of the number.
  • 1 is neither prime nor composite by mathematical convention.
  • The smallest factor found equals the number itself only when that number is prime.
  • Prime gaps grow larger on average as numbers increase, but twin primes (differing by 2) still appear.
Helpful
Not helpful
Save as image
Share
Embed
Cite
Write feedback

Formula

n is prime when no integer from 2 to √n divides it evenly (trial division primality test)

Where:

  • n=Number to test
  • d=Candidate divisor
Prime Number Calculator illustrationA teaching diagram for the Prime Number Calculator. It labels the inputs, shows a flow from setup to simplification, and highlights the formula: n is prime when no integer from 2 to the square root of n divides it evenly.Prime Number CalculatorInputsEnter a whole number \u2265 1Check it against small primesStop once past its rootFormulaSee full formula belowOutputsRead the prime/composite flagCheck the smallest factorSee the nearest primesFormulan is prime when no integer from 2 to \u221an divides it evenly(trial division primality test)Reliable workflow1. Try dividing by 22. Try odd divisors3. Stop past \u221an4. Report the result
This illustration summarizes the thinking behind the Prime Number Calculator: test divisibility only up to the square root of the number, then report whether it is prime along with its nearest prime neighbors.

Watch & Learn

A visual explanation of what prime numbers are, why they are the building blocks of arithmetic, and how they are distributed among the integers.

Worked Examples

Confirm a well-known prime

97 is a commonly used textbook example of a two-digit prime.

  1. 1Test divisors 2, 3, 5, 7 (up to √97 ≈ 9.85).
  2. 2None of them divide 97 evenly.
  3. 3Result: 97 is prime, its smallest factor is itself, next prime is 101, previous prime is 89.
Final Answer: isPrime = Yes

Identify a composite number

100 is divisible by 2, so it fails the primality test immediately.

  1. 1Test 2 first: 100 / 2 = 50, an integer, so 2 divides evenly.
  2. 2Because a divisor was found, 100 is composite.
  3. 3Result: isPrime = No, smallestFactor = 2, nextPrime = 101, previousPrime = 97.
Final Answer: isPrime = No, smallestFactor = 2

Test the smallest prime

2 is the only even prime number and a useful edge case.

  1. 1There are no integers between 2 and √2 to test.
  2. 22 is prime by definition, and it is the smallest and only even prime.
  3. 3Result: isPrime = Yes, smallestFactor = 2, previousPrime = none.
Final Answer: isPrime = Yes

Introduction

The Prime Number Calculator answers a deceptively simple question — is this number prime? — using the trial division primality test that only checks candidate divisors up to the square root of the number. Beyond a yes/no answer, it surfaces the smallest factor for composite numbers and the nearest prime neighbors, making it useful for students studying number theory, teachers building examples, and developers who need quick primality checks without writing their own sieve.

Prime Number Calculator - Illustration
Prime Number Calculator

What makes a number prime?

A prime number is a whole number greater than 1 that has exactly two positive divisors: 1 and itself. Numbers with more than two divisors are called composite, and 1 is neither prime nor composite by convention.

  • 2 is the smallest and only even prime.

  • Every other prime is odd, but not every odd number is prime.

  • 1 is excluded from both prime and composite classifications.

How the trial division test works

To test whether n is prime, the calculator checks whether any integer from 2 up to √n divides n evenly. If one does, n is composite and that divisor is the smallest factor. If none do, n must be prime, because any factor larger than √n would have to pair with a factor smaller than √n, which the search would have already found.

  • Check divisibility by 2 first to instantly rule out even composites.

  • Then check only odd candidates, since even numbers besides 2 cannot be prime.

  • Stop the search once the candidate exceeds √n.

Using the input correctly

Enter a single whole number of 1 or greater. The number 1 is a valid input but is reported as not prime, matching standard mathematical convention.

  • Only whole numbers are accepted.

  • Negative numbers and decimals are rejected.

  • Very large numbers take longer since the search scales with √n.

Reading the outputs

The primary result tells you whether the number is prime. For composite numbers, the smallest factor shows the first divisor found; for primes, this equals the number itself. The next and previous prime values help you locate a number's position within the sequence of primes.

isPrime:

Yes or No.

smallestFactor:

the smallest divisor greater than 1.

nextPrime:

the closest prime strictly greater than the input.

previousPrime:

the closest prime strictly less than the input, if one exists.

A dependable step-by-step workflow

The same four steps the calculator automates can be done by hand for smaller numbers.

  • Try dividing by 2 first.

  • Try successive odd divisors (3, 5, 7, ...).

  • Stop once the divisor exceeds √n.

  • Report the result: prime if nothing divided evenly.

Common mistakes and how to avoid them

A frequent mistake is testing divisors far beyond √n, which wastes effort since any larger factor would already have a smaller partner. Another common error is assuming all odd numbers are prime — 9, 15, and 21 are all odd and composite.

  • Don't test divisors beyond √n — it is unnecessary.

  • Don't assume oddness implies primality.

  • Remember 1 is neither prime nor composite.

Where primality testing shows up

Primality testing is foundational to cryptography, hashing algorithms, and random number generation, and it appears constantly in introductory computer science and discrete math courses as a first algorithmic exercise.

  • Generating keys for RSA and other cryptosystems.

  • Sieve algorithms for listing all primes up to a limit.

  • Hash table sizing, where prime table sizes reduce collisions.

  • Classic programming interview and homework exercises.

Reference patterns to remember

Knowing the first primes by heart speeds up manual checks and sanity-checks the calculator's output.

  • The first ten primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.

  • Twin primes differ by exactly 2, like 11 and 13.

  • Prime gaps grow larger, on average, as numbers get bigger.

NumberPrime?Smallest Factor
2Yes2
15No3
29Yes29
91No7

Quick Reference Card

Prime Number Cheat Sheet

Quick referencePrime Number Calculator

n is prime if no integer 2..√n divides it evenly

Valid range: Whole numbers of 1 or greater

Common Values

First 5 primes2, 3, 5, 7, 11
Smallest composite4
Largest 2-digit prime97
Is 1 prime?No

Watch Out

  • 1 is not prime and not composite.
  • Don't assume all odd numbers are prime.
  • Only test divisors up to √n — testing further wastes effort.
  • Check domain restrictions before trusting the final value.

Pro Tips

  • Check 2 first to instantly rule out even composites.
  • Memorize the first ten primes for fast mental checks.
  • Use the smallest factor output to quickly find full factorizations.
  • Estimate the answer mentally first so large errors stand out.

FAQs

Why do we only check divisors up to the square root?

If n has a factor larger than √n, it must pair with a factor smaller than √n, since their product equals n. So if no divisor is found up to √n, none exist beyond it either, and n must be prime.

Is 1 considered a prime number?

No. By mathematical convention, 1 is neither prime nor composite because a prime must have exactly two distinct positive divisors, and 1 only has one (itself).

What is the smallest factor for a prime number?

For a prime number, the smallest factor is the number itself, since its only divisors are 1 and itself, and the search always finds itself last when nothing smaller divides evenly.

Are all odd numbers prime?

No. While every prime except 2 is odd, many odd numbers like 9, 15, 21, and 25 are composite because they have smaller odd factors (3, 5, 3, and 5 respectively).

What happens when I enter 2?

2 is the smallest prime and the only even one. The calculator reports isPrime = Yes with no previous prime, since there is no prime smaller than 2.

How does this relate to cryptography?

Public-key systems like RSA rely on generating very large prime numbers. Fast, reliable primality testing (often using probabilistic methods for huge numbers) is essential to making that encryption possible.

Can this calculator handle very large numbers?

Trial division works well up to numbers in the billions, but for extremely large inputs the search can slow down since the number of candidates grows with the square root of the value.