ForHosting KIT · Developer Utilities

Mersenne prime checker using the Lucas-Lehmer test

This Mersenne prime checker takes a prime exponent p and determines whether the number 2^p - 1 is prime.

● BetaFree · in your browser
Use it from WebAPIEmailTelegramApp soon

It uses the Lucas-Lehmer test, the standard deterministic test specialized for Mersenne numbers, and returns the exact number, iteration count, and final residue. The exponent is validated first because a composite exponent cannot produce a Mersenne prime. That strict validation helps distinguish a malformed request from a valid test whose Mersenne number is composite, making the result useful in scripts, lessons, and reproducible mathematical checks.

Choose and validate the exponent

Start with an integer exponent p between 2 and 10,000. The checker accepts the canonical p field and the descriptive exponent alias, including an integer written as a string for text-oriented clients. Before constructing the potentially large Mersenne number, it verifies that p itself is prime by deterministic trial division. This is a required mathematical precondition, not merely an optimization: when p is composite, 2^p - 1 has an algebraic factorization and therefore cannot be a Mersenne prime. Such an input produces an invalid-input error rather than an ordinary false result. That distinction keeps automated workflows honest. A false result means a prime exponent was tested correctly and its associated Mersenne number is composite; an error means the request did not satisfy the domain of the Lucas-Lehmer test. Values outside the published range, decimals, missing fields, arrays, and nonnumeric strings are rejected clearly. Use p = 2 to check the smallest Mersenne prime, or a larger known prime exponent when exploring the sequence.

Understand the Lucas-Lehmer calculation

For a validated prime exponent, the checker forms M = 2^p - 1 using exact BigInt arithmetic. For odd prime p, it begins with s = 4 and repeatedly replaces s with s squared minus 2, reduced modulo M. Exactly p - 2 updates are performed. The Lucas-Lehmer theorem says that M is prime precisely when the final residue is zero. Because every operation is integral and modular, the answer does not depend on floating-point rounding, probabilistic witnesses, network services, or a random seed. The special exponent p = 2 corresponds to M = 3 and requires no recurrence steps; it is returned as prime with a zero residue and zero iterations. The response exposes the exact decimal Mersenne number, the Boolean primality decision, the method name, the iteration count, and the final residue. These fields let you verify not only the conclusion but also the exact endpoint of the deterministic sequence, which is useful when comparing implementations or documenting a computation.

Interpret and use the result responsibly

Read is_prime as the decision about the Mersenne number, not about the exponent. The exponent has already passed a separate primality check before a successful response is possible. A true value means the final Lucas-Lehmer residue was zero and 2^p - 1 is prime. A false value means p was prime but its Mersenne number was composite; this is common, since prime exponents are necessary but not sufficient. The mersenne string preserves every decimal digit without JSON number precision loss, so keep it as text unless your environment supports arbitrary-precision integers. The final_residue is also a decimal string for the same reason. This capability is well suited to bounded educational experiments, regression fixtures, mathematical demonstrations, and application checks. It is not an unbounded search service: the exponent ceiling limits runtime and response size, and each request evaluates one exponent. Browser use follows the same deterministic logic as the API, while automated calls cost $0.002 each. Cache stable results in high-volume systems because the output for a given exponent never changes.

Verify a candidate exponent

Confirm whether the Mersenne number associated with a prime exponent passes the exact Lucas-Lehmer criterion.

Teach modular recurrence

Show students how repeated modular squaring reaches a decisive zero or nonzero residue.

Build deterministic test fixtures

Generate stable Mersenne values, iteration counts, and residues for validating another implementation.

What happens if p is composite?

The request returns an invalid-input error because the capability requires p to be prime.

Does a prime exponent guarantee a Mersenne prime?

No. A prime exponent is necessary but not sufficient; the Lucas-Lehmer residue supplies the final decision.

Why are the Mersenne number and residue strings?

They can exceed the exact integer range of JSON numbers, so decimal strings preserve every digit.

Is the test probabilistic?

No. Lucas-Lehmer is deterministic for Mersenne numbers with prime exponents.

What does an API check cost?

Each request costs $0.002. The browser version uses the same pure calculation.

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/mersenne-prime-check

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/mersenne-prime-check \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"p":31}'
{
  "p": 31
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "numth.mersenne_prime_check",
  "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.

min_p2
max_p10000
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.

Read the full KIT documentation →