Modular Arithmetic Calculator
Modular arithmetic calculator: compute a mod n, modular addition, subtraction, multiplication, and exponentiation. Handles negative numbers, shows working steps, and explains congruences.
Full original guide (expanded)
Modular Arithmetic Calculator
Compute a mod n, modular addition, subtraction, multiplication, and exponentiation. Handles negative inputs, shows each step, and displays the congruence statement \(a \equiv r \pmod n\).
Designed for number theory, coding interviews, and cryptography 101
Uses a mathematically standard definition of the modulus that always returns a result between 0 and \(n-1\). Shows both raw and reduced values so you can spot mistakes quickly.
Author: CalcDomain Math Team
Reviewed by: Applied mathematician
Last updated: 2025
Educational use only. For security-critical cryptography or production code, validate results with dedicated big-integer libraries and peer review.
Interactive modular arithmetic calculator
For a^b (mod n), b must be a non-negative integer exponent.
Modular arithmetic is typically done with integers; decimals are supported for teaching, but many number-theory theorems assume integer inputs.
Result
- Raw expression
- –
- Result r = a (op) b
- –
- Canonical remainder
- –
The congruence statement a ≡ r (mod n) will appear here.
Step-by-step reduction
Steps explaining how the result was reduced modulo n will appear here.
What is modular arithmetic?
Modular arithmetic is sometimes called clock arithmetic. Instead of keeping track of the full value of a number, you only care about the remainder when dividing by a fixed positive integer \(n\) (the modulus).
Definition
For integers \(a\) and \(n > 1\), the value of \(a \bmod n\) is the unique integer \(r\) such that \(0 \le r \le n-1\) and
\[ a = qn + r \quad \text{for some integer } q. \]Two integers \(a\) and \(b\) are congruent modulo \(n\) if they have the same remainder: \[ a \equiv b \pmod n \quad \Longleftrightarrow \quad n \mid (a-b). \]
Modular addition, subtraction, and multiplication
Once a modulus \(n\) is fixed, you can do arithmetic “modulo \(n\)”:
(a + b) mod n = ((a mod n) + (b mod n)) mod n
(a − b) mod n = ((a mod n) − (b mod n)) mod n
(a × b) mod n = ((a mod n) × (b mod n)) mod n
These rules allow you to reduce intermediate results early. For example, with modulus 12:
17 mod 12 = 5, 23 mod 12 = 11
(17 + 23) mod 12 = 40 mod 12 = 4
also (5 + 11) mod 12 = 16 mod 12 = 4.
Modular exponentiation
In cryptography and number theory you often need to compute \(a^b \bmod n\) for large \(b\). Doing this naively can overflow quickly, so the standard approach is to reduce after each multiplication (“square-and-multiply”).
a^b mod n = ((…((a mod n)^2 mod n) × …) mod n)
The calculator uses an efficient modular exponentiation algorithm rather than computing \(a^b\) first and reducing at the end.
Negative numbers and the modulus
For negative integers, some programming languages use a remainder rule that can return negative values. In mathematics we usually prefer the modulus to be in \(\{0,1,\dots,n-1\}\). This tool adopts the mathematical convention:
−7 mod 5 = 3, because −7 = (−2)·5 + 3 and 0 ≤ 3 ≤ 4.
Worked example
Suppose you want to compute \(17^5 \bmod 12\).
- First reduce the base: \(17 \equiv 5 \pmod{12}\).
- Compute powers step by step, reducing each time:
- \(5^2 = 25 \equiv 1 \pmod{12}\)
- \(5^4 = (5^2)^2 \equiv 1^2 \equiv 1 \pmod{12}\)
- \(5^5 = 5^4 \cdot 5 \equiv 1 \cdot 5 \equiv 5 \pmod{12}\)
So \(17^5 \equiv 5 \pmod{12}\). The calculator will show exactly this sequence of reductions.
Where modular arithmetic is used
- Cryptography (RSA, Diffie–Hellman, elliptic curves).
- Hashing and checksums in computer science.
- Coding theory and error-correcting codes.
- Scheduling and clocks (days of the week, time-zones, circular data).
Modular arithmetic: FAQs
Formula (LaTeX) + variables + units
a = qn + r \quad \text{for some integer } q.
a \equiv b \pmod n \quad \Longleftrightarrow \quad n \mid (a-b).
','\
Definition For integers \(a\) and \(n > 1\), the value of \(a \bmod n\) is the unique integer \(r\) such that \(0 \le r \le n-1\) and \[ a = qn + r \quad \text{for some integer } q. \] Two integers \(a\) and \(b\) are congruent modulo \(n\) if they have the same remainder: \[ a \equiv b \pmod n \quad \Longleftrightarrow \quad n \mid (a-b). \]
(a + b) mod n = ((a mod n) + (b mod n)) mod n (a − b) mod n = ((a mod n) − (b mod n)) mod n (a × b) mod n = ((a mod n) × (b mod n)) mod n
17 mod 12 = 5, 23 mod 12 = 11 (17 + 23) mod 12 = 40 mod 12 = 4 also (5 + 11) mod 12 = 16 mod 12 = 4.
a^b mod n = ((…((a mod n)^2 mod n) × …) mod n)
−7 mod 5 = 3, because −7 = (−2)·5 + 3 and 0 ≤ 3 ≤ 4.
- 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.