Fast modular exponentiation calculator
Get the exact remainder of a huge power without overflow: 2 to the power 10 modulo 1000 is 24, and 3 to the power 5 modulo 13 is 9. The base, the exponent and the modulus can each run to 2048 digits. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Base
- 2
- Exponent
- 10
- Modulus
- 1000
- Result
- 24
Fast modular exponentiation answers a single precise question: what is the exact non-negative remainder when an integer base is raised to an integer exponent and reduced modulo m? That residue underpins RSA-style classroom demos, Diffie–Hellman key-exchange sketches, programming contest problems, and any production script that must power large integers without overflowing fixed-width types or losing precision past 2^53.
What fast modular exponentiation computes
Given integers base, exponent, and modulus m, modular exponentiation returns the unique remainder r in the closed range from zero through m minus one such that base raised to the exponent is congruent to r modulo m. In symbols that is r ≡ base^exponent (mod m). A naïve approach multiplies the base by itself exponent times and only then reduces, which inflates intermediate values far beyond ordinary floating-point or fixed-width integers and fails silently when precision is lost.
Fast modular exponentiation instead reduces after every multiply and squares the running base while scanning the exponent bits from least significant to most, so total work scales with the bit length of the exponent rather than its decimal magnitude. When m equals one every integer is congruent to zero, so the result is always zero. When the exponent is zero the result is one for any base as long as m is greater than one, matching the empty-product convention used in number theory and programming libraries.
Negative bases are first reduced into the non-negative residue system modulo m so the multiply chain never mixes signed remainders. Negative exponents are rejected because they demand modular inverses that may not exist when base and m are not coprime, and that inverse step is out of scope for this calculator.
Limits, performance, and where developers use it
Binary modular exponentiation performs a constant number of modular multiplications per bit of the exponent, so roughly doubling the exponent roughly doubles work while a larger modulus grows the cost of each multiply with digit length. Teachers use it to verify hand calculations of a^b mod m before students submit proofs. Developers wire it into property tests that assert protocols stay inside residue classes, into game or ranking systems that fold large counters modulo a prime, and into scripting pipelines that must not reimplement square-and-multiply in every language.
What you can do with it
Verify contest and homework a^b mod m answers
Students and coaches can check modular powers without hand-multiplying huge intermediates or trusting a calculator that silently loses precision past 2^53.
Fold large counters into a fixed residue range
Games, ranking systems, and sharding helpers that raise an integer then reduce modulo m get an exact remainder without floating-point rounding surprises.
FAQ
What is modular exponentiation?
It is computing base raised to an exponent and taking the remainder modulo m, written base^exponent mod m. This calculator returns that non-negative remainder as result.
Why is a negative exponent rejected?
Negative exponents mean reciprocal powers and require modular inverses, which may not exist when base and m are not coprime.
How does the fast modular exponentiation calculator work?
It uses binary square-and-multiply over BigInt: for each bit of the exponent it squares the running base modulo m and multiplies into the result when the bit is set.
Is the fast modular exponentiation calculator free?
The browser tool is free and runs locally.
What happens when the modulus is 1 or the exponent is 0?
With a modulus of 1 every number leaves remainder 0, so the result is always 0. With an exponent of 0 the result is 1 for any base, as long as the modulus is greater than 1: 7 to the power 0 modulo 5 gives 1.
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/dev/modular-exponentiation \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"base":"2","exponent":"10","modulus":"1000"}'const res = await fetch("https://api.kit.forhosting.com/dev/modular-exponentiation", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"base": "2",
"exponent": "10",
"modulus": "1000"
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/dev/modular-exponentiation",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"base": "2",
"exponent": "10",
"modulus": "1000"
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/dev/modular-exponentiation", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"base":"2","exponent":"10","modulus":"1000"}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"base":"2","exponent":"10","modulus":"1000"}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/dev/modular-exponentiation", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"base": "2",
"exponent": "10",
"modulus": "1000"
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "dev.modular_exponentiation",
"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.
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
This fast modular exponentiation calculator runs binary square-and-multiply over BigInt so intermediate products stay reduced at every step, the answer is deterministic, and you can try the same pure core free in the browser before calling the API path that bills only successful tasks.
What fast modular exponentiation computes
The modulus must be at least one; zero or negative moduli do not define a standard non-negative remainder and return invalid_input instead of inventing an answer.
How to call the API and read the result
Classic checks you can run immediately: base 2, exponent 10, modulus 1000 yields result 24; base 3, exponent 5, modulus 13 yields 9; base 7, exponent 0, modulus 5 yields 1. Send a JSON body with three required fields: base, exponent, and modulus. Each may be a decimal integer string or a JSON number; string form is preferred for values larger than JavaScript’s safe integer range because numbers above that threshold cannot represent every integer exactly. Aliases a for base, exp or e or power for exponent, and m or mod for modulus are accepted so forms and hand-written clients share one contract without renaming keys. On success the response echoes base, exponent, and modulus as normalized decimal strings and adds result as the non-negative remainder of base^exponent mod modulus. Failed validation—missing fields, non-integers, fractions, modulus below one, a negative exponent, or a value longer than the published digit limit—returns invalid_input and is never charged against prepaid balance. Pricing is a flat $0.002 per successful request under the item unit.
Limits, performance, and where developers use it
This capability caps each of base, exponent, and modulus at 2048 decimal digits—large enough for contest problems, homework, and many cryptographic-scale demos—yet small enough that pure Worker BigInt stays interactive without spawning a heavy multiprecision stack or offloading to a container. If you need million-digit moduli, Chinese remainder theorem decompositions, or constant-time side-channel-resistant crypto primitives, you need a dedicated cryptographic library; this lightweight parse tool is intentionally the shared calculator and API step for exact modular powers in development workflows. Pair it with separate factoring or modular inverse tools when a workflow needs the full modular toolkit; this endpoint answers only the power-and-reduce question so the search intent stays clear and the response stays minimal and predictable.
Integration use cases
Seed crypto demos and protocol unit tests
Scripts that need a deterministic base^exp mod m step for toy RSA, Diffie–Hellman, or residue-class assertions can call this instead of shipping their own BigInt powering.
Technical questions
What happens if the modulus is less than 1?
Modulus must be at least 1. Zero or negative moduli do not define the standard non-negative remainder used here and return invalid_input.