Modular Arithmetic Calculator

Perform modular arithmetic operations: addition, subtraction, multiplication, exponentiation, and modular inverse. Includes GCD, extended Euclidean algorithm, and RSA examples.

Modular Arithmetic Calculator

First operand
Second operand
Result
9
7^256 mod 13 = 9 (computed via repeated squaring)
a mod n
7
7 mod 13
b mod n
9
256 mod 13
gcd(a, n)
1
Coprime ✓ (inverse exists)
φ(n)
12
Euler\'s totient
Modulus
13
Elements: {0, 1, ..., 12}

Powers of 7 mod 13

ExponentResultPattern
7^01
7^17
7^210
7^35
7^49
7^511
7^612
7^76
7^83
7^98
7^104
7^112
7^121
7^137
Planning notes, formulas, and examples

About the Modular Arithmetic Calculator

The Modular Arithmetic Calculator performs the remainder-based operations used in cryptography, number theory, and algorithm design. You can evaluate a mod n, modular addition, subtraction, multiplication, exponentiation, and modular inverses without hand-running the Euclidean algorithm. It is a practical way to turn abstract congruence rules into direct calculations you can verify immediately. That is especially useful when a modular step appears inside code, proofs, or cryptography exercises and you want to confirm it quickly.

Modular arithmetic groups integers by their remainder after division. The statement "17 ≡ 5 (mod 12)" means 17 and 5 leave the same remainder when divided by 12, just like 12-hour clock arithmetic. That simple idea is the basis for RSA, checksums, pseudorandom generators, and many competitive-programming techniques.

Enter any operation and the tool returns the result together with the intermediate steps that matter: GCD, coprimality, inverse existence, and repeated-squaring reductions. That makes it useful both for verifying homework and for checking real implementation logic.

When This Page Helps

Use it to verify congruences, modular inverses, and fast exponentiation before you commit the result to code, coursework, or a cryptography exercise. It is much faster than working through repeated squaring and Euclid steps by hand. That makes it useful when you want the numeric result and the modular logic to stay aligned.

How to Use the Inputs

  1. Select the operation: mod, add, subtract, multiply, exponent, or inverse.
  2. Enter the first operand (a).
  3. Enter the second operand (b) if applicable.
  4. Enter the modulus (n).
  5. View the result along with step-by-step explanation.
  6. Use presets for common cryptographic examples.
  7. Check the modular multiplication table for the chosen modulus.
Formula used
a mod n = a - n × ⌊a/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 uses repeated squaring. Modular inverse: a⁻¹ mod n exists iff gcd(a,n) = 1.

Example Calculation

Result: 7^256 mod 13 = 9

Computing 7^256 mod 13 using repeated squaring: 7^1=7, 7^2=49≡10, 7^4≡100≡9, 7^8≡81≡3, 7^16≡9... Final result: 7^256 mod 13 = 9. Direct computation of 7^256 would be impossibly large, but modular exponentiation handles it easily.

Tips & Best Practices

  • For modular exponentiation, the result is always between 0 and n-1 regardless of how large b is.
  • Modular inverse exists only when gcd(a,n) = 1 — always check coprimality first.
  • Negative results: in math, -3 mod 7 = 4 (add the modulus). Programming languages may differ.
  • Fermat's little theorem: for prime p, a^(p-1) ≡ 1 (mod p), so a^(-1) ≡ a^(p-2) (mod p).
  • For RSA-like problems: choose two primes p,q; compute n=pq, φ=(p-1)(q-1); choose e coprime to φ; find d as e's inverse mod φ.
  • The Chinese Remainder Theorem lets you solve systems of modular congruences.

Congruences and Residue Classes

Working modulo n means every integer is represented by one of the residues 0 through n-1. Two integers are congruent when their difference is divisible by n, so they behave the same way inside modular calculations. That lets you replace large intermediate values with smaller remainders without changing the final answer.

Fast Exponentiation and Inverses

Large powers are usually evaluated with repeated squaring: square, reduce modulo n, and keep going. That turns an enormous expression like a^b mod n into a short sequence of manageable multiplications. Modular inverses come from the extended Euclidean algorithm; they exist only when the base and modulus are coprime.

Where This Shows Up

Cryptography is the best-known use case, but modular arithmetic also appears in hash tables, cyclic buffers, scheduling systems, and contest algorithms. When you can inspect the GCD, inverse, and reduction steps in one place, it is much easier to catch a wrong modulus or an invalid inverse before it propagates.

Sources & Methodology

Last updated:

Frequently Asked Questions

  • Modular arithmetic is a system where numbers "wrap around" after reaching a certain modulus. It's like clock arithmetic: 10 + 5 = 3 on a 12-hour clock. Formally, a ≡ b (mod n) means n divides (a-b).