Modular Exponentiation Calculator
Modular exponentiation calculator for a^b mod m using fast binary exponentiation. Handles very large integers with BigInt, shows step-by-step square-and-multiply for smaller exponents, and explains modular arithmetic for cryptography and number theory.
Full original guide (expanded)
Modular Exponentiation Calculator
BigInt readyCompute a^b mod m using fast binary exponentiation. Handles very large integers with JavaScript BigInt, and for smaller exponents shows the step-by-step square-and-multiply procedure used in programming contests and cryptography.
Perfect for number theory, competitive programming, and understanding how RSA and Diffie–Hellman use modular arithmetic in practice.
Fast ab mod m calculator
Any integer (positive or negative). The calculator internally reduces it modulo m.
Non-negative integer exponent. Time complexity is O(log b).
Positive integer modulus (m ≥ 1). In many applications m is prime or a product of primes.
Quick examples
What is modular exponentiation?
In modular arithmetic we work with integers modulo some positive integer \(m\). The expression \[ a^b \bmod m \] means that we first take the integer power \(a^b\) and then reduce it modulo \(m\) (i.e. take the remainder after division by \(m\)).
Naively, computing \(a^b\) and then reducing it is impossible for large \(b\): the intermediate result would have thousands of digits or more. Instead, modular exponentiation algorithms reduce modulo \(m\) at every step, keeping all intermediate values within the range \(\{0,\dots,m-1\}\).
Binary (square-and-multiply) algorithm
A standard way to compute \(a^b \bmod m\) is the binary exponentiation (or square-and-multiply) algorithm. Write the exponent \(b\) in binary:
Then we process the bits from left to right:
\( \quad \text{result} \leftarrow \text{result}^2 \bmod m\)
\( \quad \text{if } b_i = 1 \text{ then } \text{result} \leftarrow \text{result} \cdot a \bmod m \)
This uses O(\(\log b\)) multiplications, instead of \(b-1\) multiplications for the naive method. That is why it is the workhorse behind cryptographic schemes and competitive programming solutions.
Why modular exponentiation matters
- Cryptography – RSA, Diffie–Hellman, ElGamal, and many signature schemes rely on computing \(a^b \bmod m\) for very large integers.
- Number theory – tools like Fermat’s little theorem and Euler’s theorem use modular exponentiation to study structure in \(\mathbb{Z}_m\).
- Programming – problems involving combinatorics “mod 10⁹+7” or “mod 998244353” are standard in contests; you almost never compute \(a^b\) directly.
Frequently asked questions
Formula (LaTeX) + variables + units
a^b \bmod m
b = (b_{k-1}b_{k-2}\dots b_1 b_0)_2, \quad b_i \in \{0,1\}.
\text{result} \leftarrow 1
','\
\[ b = (b_{k-1}b_{k-2}\dots b_1 b_0)_2, \quad b_i \in \{0,1\}. \]
\[ \text{result} \leftarrow 1 \] For each bit \(b_i\): \( \quad \text{result} \leftarrow \text{result}^2 \bmod m\) \( \quad \text{if } b_i = 1 \text{ then } \text{result} \leftarrow \text{result} \cdot a \bmod m \)
- No variables provided in audit spec.
- NIST — Weights and measures — nist.gov · Accessed 2026-01-19
https://www.nist.gov/pml/weights-and-measures - FTC — Consumer advice — consumer.ftc.gov · Accessed 2026-01-19
https://consumer.ftc.gov/
Last code update: 2026-01-19
- Initial audit spec draft generated from HTML extraction (review required).
- Verify formulas match the calculator engine and convert any text-only formulas to LaTeX.
- Confirm sources are authoritative and relevant to the calculator methodology.