ForHosting KIT · Math

Previous prime finder

Finds the largest prime strictly below your number: for 10 the primes below are 2, 3, 5 and 7, so the answer is 7. It works for any whole number above 2 up to one trillion and gives the same exact answer every time. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
N
10
You get
Previous prime
7

Looking for the prime that sits immediately below a given integer is a common step in number theory homework, contest problems, and cryptographic experiments that need a nearby prime seed.

What “previous prime” means and when it is defined

The previous prime of an integer n is the largest prime number that is strictly smaller than n. For example, when n is 10 the primes below it are 2, 3, 5, and 7, so the previous prime is 7. When n itself is prime—say n equals 17—the previous prime is still the one below it, which is 13, not 17; the definition never returns n.

The notion is only defined when at least one prime exists below n. Because 2 is the smallest prime, no integer less than or equal to 2 has a previous prime: there is nothing strictly smaller that is prime. Internally the engine walks candidates from n minus one downward and tests each with a deterministic primality check until the first prime appears.

Prime gaps, limits, and practical uses

Prime gaps grow slowly on average: near a size x the typical distance between consecutive primes is on the order of the natural log of x, so a short downward walk from n usually finds the previous prime after only a few candidates. If you need the previous prime of a cryptographic-size integer, you need a dedicated big-integer stack and a stronger primality engine; this lightweight parse capability is not that tool.

In practice teachers use the previous prime to illustrate Bertrand-type questions, students use it to verify hand calculations before submitting proofs, and developers use it when a protocol or game rule needs the greatest prime below a level, score, or bucket size.

Check homework and contest “greatest prime below n” answers

Students and coaches can verify the largest prime less than a bound without hand-listing primes or risking off-by-one mistakes when n itself is prime.

Seed nearby primes in scripts and property tests

Generators that need a prime strictly below a size, score, or table length can call this deterministic step instead of shipping their own trial division.

What is the previous prime of n?

It is the largest prime strictly less than n.

Is the previous prime finder free?

The browser tool is free and runs locally.

What is the maximum n?

n must be an integer greater than 2 and at most 10^12 (one trillion). Larger values need a multiprecision primality stack outside this capability.

What happens if my number is already prime?

You still get the prime below it, never the number itself. For 17 the answer is 13, because the previous prime must be strictly smaller than the number you enter.

Why is there no previous prime for 2?

Because 2 is the smallest prime, so nothing below it is prime. That is why the number must be greater than 2; for 3 the answer is 2.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/numth/prev-prime

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/numth/prev-prime \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"n":10}'
{
  "n": 10
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "numth.prev_prime",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_n1000000000000
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

This previous prime finder takes an integer n greater than 2 and returns the largest prime strictly less than n—deterministic trial division, no sampling, and free to try in your browser before you wire the same core into scripts through the API.

What “previous prime” means and when it is defined

This capability therefore requires n greater than 2 and answers with invalid_input if you send 2, 1, 0, or a negative value. That contract matches textbooks and programming contest statements that ask for the greatest prime less than n without inventing an answer where none exists. You never get a probabilistic Miller–Rabin maybe; for every accepted n in range the integer previous_prime is exact and reproducible across browser and API paths.

How to call the API and what comes back

Use it from homework pipelines, property-based test generators that need a nearby prime bound, or any workflow that wants a reproducible predecessor without installing a computer-algebra system or writing trial division by hand again. Send a JSON body with a single required field n—an integer strictly greater than 2 and at most one trillion (10^12). Aliases number and value are accepted for the same field so forms and hand-written clients can use whichever label is natural. On success the response includes n as accepted and previous_prime as the largest prime strictly less than that n. Typical checks: n of 3 yields previous_prime 2; n of 100 yields 97; n of 1000 yields 997. Failed validation—missing n, a non-integer, a fraction, a value at or below 2, or a value above the published maximum—returns invalid_input and is never charged. Pricing is a flat $0.002 per successful request (unit item with zero marginal unit price).

Prime gaps, limits, and practical uses

Pathological gaps still exist, which is why this capability caps n at one trillion—large enough for classroom problems, many programming contests, and lightweight crypto experiments, yet small enough that trial-division primality stays interactive on the pure Worker path without multiprecision libraries. Pair it with a next-prime routine if you need the full neighboring pair; this endpoint deliberately answers only the strictly-less-than side so the search intent stays clear and the response stays minimal.

Illustrate prime gaps in teaching materials

Compare n with previous_prime across a range of values to show how gaps behave without building a full prime table in the lesson notebook.

Why is n = 2 rejected?

2 is the smallest prime, so no prime exists strictly below it. Values of n less than or equal to 2 return invalid_input.

How does the previous prime finder work?

It walks candidates from n − 1 downward and tests each with deterministic trial division until it finds a prime, then returns that integer as previous_prime.

Read the full KIT documentation →