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 ready

Compute 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.

Step view is auto-disabled if b has too many bits.

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:

\[ b = (b_{k-1}b_{k-2}\dots b_1 b_0)_2, \quad b_i \in \{0,1\}. \]

Then we process the bits from left to right:

\[ \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 \)

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


Audit: Complete
Formula (LaTeX) + variables + units
This section shows the formulas used by the calculator engine, plus variable definitions and units.
Formula (extracted LaTeX)
\[a^b \bmod m\]
a^b \bmod m
Formula (extracted LaTeX)
\[b = (b_{k-1}b_{k-2}\dots b_1 b_0)_2, \quad b_i \in \{0,1\}.\]
b = (b_{k-1}b_{k-2}\dots b_1 b_0)_2, \quad b_i \in \{0,1\}.
Formula (extracted LaTeX)
\[\text{result} \leftarrow 1\]
\text{result} \leftarrow 1
Formula (extracted LaTeX)
\[','\\]
','\
Formula (extracted text)
\[ b = (b_{k-1}b_{k-2}\dots b_1 b_0)_2, \quad b_i \in \{0,1\}. \]
Formula (extracted text)
\[ \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 \)
Variables and units
  • No variables provided in audit spec.
Sources (authoritative):
Changelog
Version: 0.1.0-draft
Last code update: 2026-01-19
0.1.0-draft · 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.
Verified by Ugo Candido on 2026-01-19
Profile · LinkedIn
Formulas

(Formulas preserved from original page content, if present.)

Version 0.1.0-draft
Citations

Add authoritative sources relevant to this calculator (standards bodies, manuals, official docs).

Changelog
  • 0.1.0-draft — 2026-01-19: Initial draft (review required).