Skip to content
🛠️ToolsShed

Modular Arithmetic Calculator

Perform modular arithmetic: mod, inverse, Euler phi, and more.

About this tool

Modular arithmetic is a fundamental branch of mathematics dealing with remainders and cyclical patterns. This calculator helps you perform core modular operations—finding remainders, computing modular inverses, calculating Euler's totient function, and solving modular equations—all without needing advanced mathematical software. Whether you're studying cryptography, number theory, or computer science, understanding how numbers behave under modular constraints is essential.

Using this tool is straightforward: select the operation you need, enter your numbers (base, modulus, exponent, or other parameters depending on the operation), and get instant results. For example, to find 17 mod 5, you would select the modulo operation, enter 17 and 5, and immediately see the result of 2. The calculator handles both simple remainder calculations and more advanced operations like modular exponentiation (useful in encryption) and finding multiplicative inverses (crucial for solving congruences).

This modular arithmetic calculator is ideal for students learning discrete mathematics, cryptographers working with RSA and other encryption schemes, programmers implementing hash functions, and anyone exploring number theory. The tool saves time on repetitive hand calculations and eliminates arithmetic errors, letting you focus on understanding the mathematical concepts behind modular systems.

Frequently Asked Questions

Code Implementation

# Modular arithmetic operations

def mod_pow(base: int, exp: int, mod: int) -> int:
    """Fast modular exponentiation: base^exp mod m — O(log exp)."""
    return pow(base, exp, mod)  # Python built-in uses fast algorithm

def extended_gcd(a: int, b: int):
    """Extended Euclidean: returns (gcd, x, y) where a*x + b*y = gcd."""
    if b == 0:
        return a, 1, 0
    g, x, y = extended_gcd(b, a % b)
    return g, y, x - (a // b) * y

def mod_inverse(a: int, m: int) -> int:
    """Modular inverse of a mod m (exists only if gcd(a,m) == 1)."""
    g, x, _ = extended_gcd(a % m, m)
    if g != 1:
        raise ValueError(f"Inverse of {a} mod {m} does not exist")
    return x % m

def chinese_remainder(remainders, moduli):
    """Solve x ≡ r_i (mod m_i) using CRT (moduli must be pairwise coprime)."""
    N = 1
    for m in moduli:
        N *= m
    result = 0
    for r, m in zip(remainders, moduli):
        Ni = N // m
        result += r * Ni * mod_inverse(Ni, m)
    return result % N

# Examples
print(mod_pow(2, 10, 1000))          # 24  (2^10 = 1024 → 1024 mod 1000 = 24)
print(mod_inverse(3, 11))            # 4   (3 × 4 = 12 ≡ 1 mod 11)
print(chinese_remainder([2, 3, 2], [3, 5, 7]))  # 23 (x≡2 mod3, x≡3 mod5, x≡2 mod7)

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.