Modulo Calculator & Big Integer Modular Exponentiation

Find a mod n for any size of whole number, including negative numbers, and calculate modular powers aᵇ mod n quickly.

Updated · Free · Runs in your browser

Calculate
Result —
Division —
Truncated remainder —
Euclidean modulo —
Meaning —
    Was this calculator helpful?

    The formula

    a mod n = r, where a = n × q + r
    Euclidean (mathematical) modulo: 0 ≤ r < |n|
    Truncated remainder (% in C, Java, JavaScript): r has the same sign as a
    Modular exponentiation: aᵇ mod n by square-and-multiply — square for each bit of b and multiply when the bit is 1

    Worked example

    1. −17 mod 5:
    2. Euclidean: −17 = 5 × (−4) + 3, so −17 mod 5 = 3.
    3. Truncated (JavaScript −17 % 5): −17 = 5 × (−3) − 2, giving −2.
    4. Modular power: 3^65537 mod 5. Since 3⁴ ≡ 1 (mod 5) and 65537 = 4 × 16384 + 1, the result is 3¹ = 3.

    What is modulo?

    The modulo operation gives the remainder after division. 17 mod 5 is 2, because 17 = 5 × 3 + 2. It's often called clock arithmetic, because numbers wrap around once they reach the modulus, just like hours on a clock: 15:00 is 3 o'clock because 15 mod 12 = 3. Modulo is used in calendars, to find the day of the week; in programming, to cycle through arrays, alternate colours and check whether a number is even; and in cryptography, hashing and check digits on barcodes, ISBNs and credit cards.

    Negative numbers: two conventions

    For negative numbers, programming languages disagree. In C, C++, Java, JavaScript and C#, the % operator returns a truncated remainder with the same sign as the dividend, so −17 % 5 is −2. In Python, Ruby and mathematics, the result has the same sign as the divisor, so −17 mod 5 is 3. Both satisfy a = n × q + r, but with different quotients. The calculator shows both. If you need a non-negative result in JavaScript, use ((a % n) + n) % n.

    Big integers

    Because the calculator uses arbitrary-precision BigInt arithmetic, you can enter numbers with hundreds of digits and get exact results. Normal floating-point numbers in JavaScript lose precision above 2^53 (about 9 × 10^15), so 12345678901234567890 % 7 would give the wrong answer with ordinary numbers. Exact big-integer arithmetic matters for cryptography, for verifying IBAN bank account numbers (which uses mod 97 on a 30-digit number) and for number theory puzzles.

    Modular exponentiation

    Computing aᵇ mod n directly is impossible for large exponents: 3^65537 has more than 31,000 digits. Square-and-multiply avoids this by reducing modulo n after every step. It writes the exponent in binary, squares the running result for each bit and multiplies by a when the bit is 1, so the number of steps grows only with the number of digits in the exponent. This is how RSA encryption works: a message m is encrypted as m^e mod n, with e often 65537, and decrypted with a secret exponent d.

    Modular arithmetic rules

    Modular arithmetic follows familiar rules: (a + b) mod n = ((a mod n) + (b mod n)) mod n, and the same is true for multiplication. This lets you work with small remainders instead of huge numbers. Fermat's little theorem says that if p is prime and a isn't a multiple of p, then a^(p−1) mod p = 1, which is the basis of fast primality tests. Two numbers are congruent modulo n, written a ≡ b (mod n), when they have the same remainder. To check whether a number is prime, use our prime factorization calculator.

    Everyday uses

    To find the day of the week 100 days from a Monday, calculate 100 mod 7 = 2, so it's a Wednesday. To work out where a 30-hour flight lands on a 24-hour clock, add the hours mod 24. Check digits on ISBN-10 numbers use mod 11, and Luhn's algorithm for credit card numbers uses mod 10. For conversions between time zones, our time zone converter handles the wrap-around for you, and the GCD and LCM calculator finds when cycles line up.

    Frequently asked questions

    What does mod mean in maths?
    a mod n is the remainder when a is divided by n. For example, 23 mod 7 = 2, because 23 = 3 × 7 + 2.
    What is −7 mod 3?
    In the mathematical convention it's 2, because −7 = 3 × (−3) + 2. In C, Java and JavaScript, −7 % 3 gives −1.
    What is the difference between remainder and modulo?
    For positive numbers they're the same. For negatives, the remainder (truncated division) keeps the sign of the dividend, while modulo (floored or Euclidean division) gives a result between 0 and n − 1.
    How do I calculate a large power mod n?
    Use square-and-multiply: repeatedly square the base mod n and multiply in the bits of the exponent. The calculator does this exactly, even for exponents with hundreds of digits.
    Why is modulo used in cryptography?
    Modular arithmetic keeps numbers within a fixed range, and some operations, like modular exponentiation, are easy to do but very hard to reverse without a secret key.

    ↑↓ to navigate · Enter to open