Modular Arithmetic Calculator
Compute a mod n, modular addition, multiplication and power (aᵇ mod n) — clock arithmetic made instant.
1,223 views
How Modular Arithmetic Works
a mod n asks one simple question: what remainder is left after dividing a by n? Formally, a mod n = a − n × floor(a ÷ n), and by mathematical convention the result always lands between 0 and n−1. The most intuitive way to picture this is a clock face: hours do not count forever, they wrap around at 12 (or 24). Say it is 15:00 and you need to know the time 10 hours later — you do not get "25:00," you compute (15 + 10) mod 24 = 1, which is 01:00 the next day. Every value cycles back through the same set of "slots," and that wrap-around is exactly what n does in a mod n.
This calculator extends the same idea to three related operations: modular addition (a + b) mod n, modular multiplication (a × b) mod n, and modular exponentiation aᵇ mod n. The last one looks innocent but is the single most important operation in modern cryptography. Computing it the naive way — raising a to the power b first, then taking the remainder — would produce numbers with millions of digits for real-world key sizes, long before the modulo step ever runs. Instead, the tool uses the square-and-multiply algorithm: it repeatedly squares the base and reduces modulo n at every single step, so the intermediate numbers never grow beyond the size of n itself, no matter how large b is. That is what makes exact results possible even for exponents hundreds of digits long.
What You Should Know
- Cryptography: RSA and Diffie-Hellman key exchange both run on modular exponentiation with very large primes — encrypting a message is, at its core, computing aᵇ mod n where a, b and n are each hundreds of digits long.
- Hash functions: most hashing schemes reduce an input of arbitrary size down to a fixed-size "bucket" using a modulo operation, which is why mod turns up throughout hash tables, checksums and load-balancing logic.
- Calendars and clocks: weekday calculations, 12/24-hour time conversions and calendar cycles are all modular arithmetic in disguise — the "clock arithmetic" nickname comes directly from this.
- Sign conventions differ: mathematics always defines a mod n as non-negative, but many programming languages (C, JavaScript, Java) instead return a remainder with the same sign as a, which can trip up direct translations of formulas into code.
Frequently Asked Questions
What is −7 mod 3?
This tool follows the mathematical convention where the result is always non-negative: −7 mod 3 = 2 (since −7 = −3×3 + 2). Some programming languages return −1 instead, because they define the remainder to carry the same sign as the dividend rather than always being positive.
Where is aᵇ mod n used in real life?
It is the core operation of RSA and Diffie-Hellman key exchange: encrypting a message or agreeing on a shared secret essentially means computing a huge modular power, often with numbers 2048 bits long or more.
Why does the clock example use mod 24 and not mod 12?
Both work — a 12-hour clock wraps at mod 12, a 24-hour clock at mod 24. The modulus n is simply the size of the cycle you care about; pick whichever clock convention matches how you are counting time.
Is a mod n the same as integer division?
They are two halves of the same division. a ÷ n (integer division) gives the quotient — how many whole times n fits into a — while a mod n gives what is left over. Together, quotient × n + remainder always reconstructs a.
Why is square-and-multiply faster than computing aᵇ directly?
Direct exponentiation multiplies a by itself b−1 times, and the intermediate value explodes in size long before you can reduce it modulo n. Square-and-multiply instead reduces after every squaring step, so the numbers involved never grow past roughly the size of n — turning an operation that would take longer than the age of the universe for cryptographic key sizes into one that finishes in milliseconds.
Similar Tools
Report a Problem
Modular Arithmetic Calculator
Comments
No comments yet — be the first to write one!