Skip to main content
CalcMax

Prime Number Calculator

Range: 2 – 1,000,000

Result

2Prime

Number of divisors

Previous prime
97
Next prime
97

A prime number is a whole number greater than 1 whose only positive divisors are 1 and itself. Two, three, five, seven, eleven and thirteen are prime. Four is not, because 2 divides it; nine is not, because 3 does; one is not either, and the reason is a definition rather than a calculation — one has only a single divisor, so it fails the requirement of having exactly two. This page answers the yes-or-no question with a badge, reports the number of divisors that settled it, and gives the nearest prime on each side. The divisor count is the whole test: a prime has exactly two divisors and a composite number has more, so the number on the first row is both the evidence and the answer. The neighbours are worth having because they answer the question people actually ask next. If a number is not prime, the useful follow-up is what the closest primes are, which matters when you are picking a modulus or a hash table size and want a prime near a number you already have in mind. Both neighbours are inclusive: 97 is prime, so its previous prime and its next prime are both 97. That is deliberate rather than an oversight, since a rule like strictly below would leave the prime case with nothing to print. One case sits outside the range the page accepts: the next prime after one million is 1000003, so a question asked inside the range can have an answer outside it, and the page reports it rather than refusing.

The two verdicts, with the divisor count behind each one

VerdictDivisor countExample
Primeexactly 2 divisors97
Composite3 or more divisors100

Two rows, and between them they cover every whole number above 1. The middle column is the test: exactly two divisors means prime, three or more means composite, and nothing else has to be checked. That is why the badge on the result panel reads the same number the row beside it prints rather than running a second calculation — with one criterion there is nothing for the two to disagree about. The examples are one of each: 97 has only 1 and 97 as divisors, while 100 has nine divisors because 2, 4, 5, 10, 20, 25 and 50 all divide it as well. Note that both example counts are printed as whole numbers with no separators, so a large divisor count prints in full rather than in a shortened form.

Four numbers, with the nearest prime on each side

NumberPrevious primeNext prime
252329
979797
10097101
10000009999831000003

Read the second row first, because it is the surprising one: 97 is prime, and both of its neighbours come back as 97. That is the inclusive rule in action — the largest prime no bigger than 97 is 97, and the smallest prime no smaller than 97 is 97 too. The rule exists so that the prime case has an answer at all; a strict inequality would leave these two rows empty on exactly the inputs where the verdict is most certain. The first row is a number in the middle of a gap: 25 sits between 23 and 29, four apart on the high side and two on the low. The third row has 100 between 97 and 101, and the fourth is the input ceiling, where the next prime is 1000003 — larger than any number the page accepts, and reported anyway, because the answer to a question asked inside the range is allowed to sit outside it. The largest jump in the table is the last row's twenty, between 999983 and 1000003 — bigger than the others, which is what prime gaps do as the numbers grow, slowly and irregularly rather than on any schedule.

Formula

n is prime <=> d(n) = 2; previousPrime(97) = 97; nextPrime(97) = 97; nextPrime(1000000) = 1000003

n
The whole number being tested — from 2 to 1000000. The lower bound is deliberate rather than inherited: the previous prime of 1 does not exist, so a page that accepted 1 would have a row it could not fill honestly. Whether 1 is prime is a definition question and is answered in the questions below rather than by the calculator
d(n)
The number of positive divisors, which is the first row of the result and the only evidence the verdict rests on. d(n) = 2 means exactly two numbers divide it, which is the definition of prime. The count comes from the shared number theory routine, so it is the same value the factor page and the prime factorization page report for the same input
d(n) = 2
The test itself, stated as an equation. It is an equivalence and not an approximation: a number is prime if and only if it has exactly two divisors. For 97 the divisors are 1 and 97, so the count is 2 and the badge reads prime. For 100 they are 1, 2, 4, 5, 10, 20, 25, 50 and 100, so the count is 9 and the badge reads composite
previousPrime(n)
The largest prime that is less than or equal to n. It is inclusive at the top, so when n is prime the answer is n itself. For 100 the answer is 97; for 25 it is 23; for 97 it is 97. The interval is closed because the alternative would need a rule for what to print when n is already prime, and a blank row in a result panel reads as a failure rather than as a fact
nextPrime(n)
The smallest prime that is greater than or equal to n, with the same inclusive rule at the bottom. For 25 it is 29, for 100 it is 101, and for 97 it is 97. This one can leave the range of the input: nextPrime(1000000) is 1000003, a prime larger than any number the page accepts, and it is reported as the answer rather than treated as out of bounds
1e6 to 1e6 + 100
The neighbourhood of the input ceiling, and why a separate test is needed up there. The primes near a million are 999983 and 1000003, so the search from 1000000 has to look past a million in one direction. The routine that counts divisors refuses arguments above a million and would throw, so the neighbour search uses its own test that has no such limit — and the two have to agree wherever they overlap, which is what the prime rows in the examples check

Picking a modulus is the most practical reason to want a prime near a number you have chosen. The number of slots in a hash table is usually taken to be prime, because a prime modulus spreads keys that share a common factor instead of letting them collide; a table sized 1000 puts every multiple of 25 into the same few slots, while one sized 997 does not. The same instinct applies in cryptography, where keys are built from primes that are large and far apart. Checking whether a number is prime also settles divisibility questions quickly: if a number has no prime divisor up to its square root, it has none at all, and the badge answers that in one step rather than by trial. Some puzzles are simply about primality — twin primes, the gaps between consecutive primes, and whether a given number is the product of two primes. Where the question turns out to be about the factors themselves, the factorization page breaks the number into primes and is the natural next stop; where it is about which numbers divide yours, the factor page lists them all; and where the number being tested is not prime and you want to know what it is made of, the count of divisors on this page is the first clue rather than the full answer.

Worked examples

  1. A prime: 97

    1. Test the divisors of 97: 2 does not divide it, and neither does 3, 5, 7 or 11
    2. Stop at the square root: 10 × 10 = 100 is already past 97, so there is nothing left to test
    3. The only divisors are 1 and 97, so the count is 2 and the number is prime
    4. The previous prime is 97 itself, because 97 is already prime and the search is inclusive
    5. The next prime is also 97, for the same reason

    The default input, and the cleanest illustration of the inclusive rule. Both neighbours come back as the number itself, which looks at first like the rows failed to do anything. They did: the largest prime no bigger than 97 is 97, and the smallest prime no smaller than 97 is 97 as well. The alternative — a strict inequality — would leave these two rows with nothing to print on exactly the inputs where the page is most confident, and a blank row in a result panel reads as an error. This case is also where the two independent tests on the page meet: the divisor count says 2, and the neighbour search agrees that 97 is prime, and they use different code to get there.

  2. A composite number: 100

    1. 100 is even, so 2 divides it; it ends in 00, so 4, 5, 10, 20, 25 and 50 divide it as well
    2. The divisors are 1, 2, 4, 5, 10, 20, 25, 50 and 100 — nine of them
    3. Nine is more than two, so the badge reads composite rather than prime
    4. The largest prime at or below 100 is 97; the smallest at or above it is 101
    5. Both neighbours are one step outside the number, which is what being composite in the middle of a gap looks like

    The case that shows the neighbours doing real work. When a number is composite the two rows are the useful output, because they answer the question a reader has next: if not this number, then which? Ninety-seven and one hundred and one are the closest primes, and 100 sits between them. The divisor count of nine is also worth a look — it is odd, which happens exactly when the number is a perfect square, and 100 is 10 squared. So a single glance at the count already tells you something about the shape of the number, before any factoring is done.

  3. A number just past a prime: 25

    1. The divisors of 25 are 1, 5 and 25 — three of them, because 5 pairs with itself
    2. Three is more than two, so 25 is composite
    3. Walk down from 25: 24, 23 — 23 is prime, so it is the previous prime
    4. Walk up from 25: 26, 27, 28, 29 — 29 is prime, so it is the next prime
    5. The gap here is six in total: 23 and 29 straddle 25

    A perfect square, which is why the divisor count is odd, and a case where the two neighbours are at visibly different distances — two below and four above. The count of three also shows why two is the right threshold rather than a count of prime factors: 25 has just one prime factor, 5, but it is not prime, and the divisor count catches that without needing to look at the factorization at all.

Limitations

The input must be a whole number from 2 to 1000000. Zero and one are refused, and one is refused for a different reason than zero: it is a definition question rather than an out-of-range number, and the previous prime of 1 does not exist. Negative numbers are refused — primality is a property of whole numbers above 1, and while a convention exists for negative primes in some branches of mathematics, this page does not adopt one. Decimals are refused rather than rounded. The ceiling of one million applies to the input only; the two neighbour rows may legitimately report a prime outside it, and the next prime after a million is 1000003, which is reported rather than refused. The test behind the verdict is trial division up to the square root, which is instant at this size and hopeless on a number with twenty digits, and that boundary is a property of the problem rather than of this implementation. The page reports three numbers and a badge: it does not list the divisors themselves, does not factorize a composite number, and tests one number at a time rather than a range. The reference tables below are fixed rows rather than a response to your input. Finally, a prime is reported as its own previous and next prime, which is a deliberate choice of an inclusive interval and not two rows that failed to find anything.

Frequently asked questions

Is 1 a prime number?
No, and it is not a composite number either. A prime is defined as a whole number greater than 1 with exactly two positive divisors, and 1 has only one divisor, so it fails the definition on both counts. This is a choice made deliberately rather than an oversight: if 1 were counted as prime, the statement that every number has exactly one prime factorization would stop being true, because you could multiply a factorization by 1 as many times as you liked. Excluding 1 is what keeps that theorem clean. Because it is a matter of definition rather than of arithmetic, the page does not accept 1 as an input — the answer lives here instead.
Why do the previous and next prime both come back as the number itself?
Because both searches are inclusive. The previous prime is the largest prime that is not bigger than your number, and the next prime is the smallest prime that is not smaller than it. When the number is already prime, it satisfies both descriptions, so both rows report it. The alternative would be a strict inequality, and then a prime input would leave two rows with nothing to print. A blank row in a result panel reads as something going wrong, and the page would be unable to answer the one case where it is most sure of itself. The same convention appears in rounding, where a number already at the target precision comes back unchanged.
Why can the next prime be larger than a million when the input cannot?
Because the ceiling is a limit on what you can ask, not on what the answer may be. The next prime after 1000000 is 1000003, and refusing to print it would mean refusing to answer a perfectly well-posed question about an input the page accepted. So the neighbour search runs on its own test, with no upper bound, while the divisor count keeps using the shared routine that only covers the range. That does mean two pieces of logic both decide whether a number is prime — one bounded, one not — and they are required to agree wherever they overlap, which is what the 97 example checks: the count says 2, and the neighbour search says 97 is prime.
What is a prime number actually used for?
Sizing things, mostly. Hash tables are usually given a prime number of slots, because a prime modulus spreads out keys that share a common factor — a table with 1000 slots sends every multiple of 25 into the same few positions, while one with 997 slots does not. The same reasoning applies anywhere a counter wraps around: a cycle length that is prime avoids resonating with regular patterns in the data. Cryptography is the other big one, where keys are built from primes that are both very large and far apart, and the security rests on how hard it is to factor their product back into the two primes it came from. Smaller uses are everywhere: checking a divisibility claim, testing whether a number is the product of two primes, and the classic puzzles about twin primes and the gaps between consecutive primes.
How does the page decide, and how sure is it?
By counting divisors, which is exact rather than probabilistic. A number is prime if and only if it has exactly two positive divisors, so the count settles the question with no possibility of a wrong answer, and no need to trust a test that might be fooled. The counting is done by trial division up to the square root, which is why a million is the ceiling: past that the method becomes slow rather than unreliable. For much larger numbers the exact methods are genuinely impractical and probabilistic tests are used instead, but at this size there is no reason to accept anything less than certainty, and the page does not.
Why is the divisor count shown rather than just the verdict?
Because the count is the reason for the verdict, and showing it means the two can never disagree — the badge is not a second calculation but a reading of the number printed next to it. It is also useful on its own. An odd count means the number is a perfect square, since the square root pairs with itself rather than with a different divisor. A count of 2 is the definition of prime. A count that is large relative to the number's size says it has many small factors, which is the kind of number that collects divisors quickly. And it connects the page to the others: the prime factorization page reports the same divisor count for the same input, working it out from the exponents instead, so the two pages check each other.

References

Related calculators