Mersenne prime checker using the Lucas-Lehmer test
Give a prime exponent and the Lucas–Lehmer test tells you whether 2^p − 1 is prime: for 31 it runs 29 steps, ends with a residue of 0 and confirms the prime 2147483647. You see the exact number, not just a yes or no. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Power (W)
- 31
- Mersenne
- 2147483647
- Iterations
- 29
- Final residue
- 0
This Mersenne prime checker takes a prime exponent p and determines whether the number 2^p - 1 is prime. 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.
How the Lucas–Lehmer test decides
A Mersenne number has the form 2^p − 1, and it can only be prime when the exponent itself is prime. That is why the checker first confirms that the exponent is prime and refuses a composite one instead of answering false. It then builds the exact number and runs the Lucas–Lehmer sequence: start with 4, square the value, subtract 2 and keep only the remainder after dividing by the Mersenne number. After the exponent minus 2 such steps, the number is prime exactly when the final remainder is zero. For the exponent 31 that means 29 steps, a final residue of 0 and the prime 2147483647.
When to use it and what to watch for
Use it to confirm a candidate exponent, to check a table of known Mersenne primes or to show students how repeated squaring with remainders reaches a zero or nonzero result. Two mix-ups are common. A prime exponent does not guarantee a prime result, so a false answer is normal and simply means the Mersenne number is composite. An error is different: it means the exponent is not prime, is not a whole number or falls outside 2 to 10,000. The exponent 2 is a special case that gives 3, prime with zero steps and a residue of zero.
Choose and validate the exponent
Start with an integer exponent p between 2 and 10,000. 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. Values outside the published range, decimals, missing fields, arrays, and nonnumeric strings are rejected clearly.
What you can do with it
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.
FAQ
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.
Is the test probabilistic?
No. Lucas-Lehmer is deterministic for Mersenne numbers with prime exponents.
What happens if the exponent is composite?
You get an error instead of a false answer. A composite exponent always gives a composite Mersenne number, so it is outside what the Lucas–Lehmer test decides. Enter a prime exponent between 2 and 10,000.
Is 2^31 − 1 a Mersenne prime?
Yes. With the exponent 31 the checker builds 2147483647, runs 29 Lucas–Lehmer steps and ends with a residue of 0, which proves the number is prime.
What does a false result mean?
It means a prime exponent was tested correctly and its Mersenne number turned out to be composite. That is a normal outcome, unlike an error, which only appears when the exponent is not a valid prime in range.
Related tools
For developers — API access
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.
API endpoint
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.
Call it from your stack
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}'const res = await fetch("https://api.kit.forhosting.com/numth/mersenne-prime-check", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"p": 31
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/mersenne-prime-check",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"p": 31
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/mersenne-prime-check", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"p":31}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"p":31}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/mersenne-prime-check", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"p": 31
}Example response
{
"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.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
min_p | 2 |
max_p | 10000 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
Choose and validate the exponent
The checker accepts the canonical p field and the descriptive exponent alias, including an integer written as a string for text-oriented clients. 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. Use p = 2 to check the smallest Mersenne prime, or a larger known prime exponent when exploring the sequence.
Understand the Lucas-Lehmer calculation
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. 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. 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
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. Cache stable results in high-volume systems because the output for a given exponent never changes. Read is_prime as the decision about the Mersenne number, not about the exponent. 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. 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.
Integration use cases
Build deterministic test fixtures
Generate stable Mersenne values, iteration counts, and residues for validating another implementation.
Technical questions
What happens if p is composite?
The request returns an invalid-input error because the capability requires p to be prime.
Why are the Mersenne number and residue strings?
They can exceed the exact integer range of JSON numbers, so decimal strings preserve every digit.
What does an API check cost?
Each request costs $0.002. The browser version uses the same pure calculation.