Loading Calculator...
Please wait a moment
Please wait a moment
Calculate modulo with clock arithmetic and programming applications
The modulo operation finds the remainder after division of one number by another. It's written as "a mod n" and returns the remainder when a is divided by n. The result is always in the range [0, n-1] for positive n.
Examples:
While often used interchangeably, modulo and remainder can differ for negative numbers:
Think of modulo as a clock. On a 12-hour clock, if it's 10 o'clock and you wait 5 hours, it becomes 3 o'clock (not 15 o'clock). This is because 15 mod 12 = 3.
Common Examples:
RSA encryption uses modular arithmetic with large prime numbers to secure data. The security relies on the difficulty of factoring large numbers.
Hash tables use modulo to map keys to array indices: index = hash(key) mod table_size
In programming, circular buffers use modulo to wrap the index back to 0 when reaching the end of the array.
Credit card numbers, ISBN numbers, and barcodes use modulo operations to generate and verify check digits for error detection.
n mod 2 quickly tells if a number is odd (1) or even (0), used extensively in algorithms and game programming.
The % operator in most programming languages (C, Java, JavaScript) computes the remainder, which can be negative. Mathematical modulo always returns non-negative results. Python's % operator implements true modulo (Euclidean division). For positive numbers, they're identical.
Modulo with large primes creates a one-way function - easy to compute but hard to reverse. For example, in RSA encryption, you can easily compute m^e mod n, but finding m from the result without knowing the private key is extremely difficult for large numbers.
For a mod n where a is negative, add multiples of n until you get a positive result in [0, n-1]. For example, -17 mod 5: add 5 repeatedly: -17 + 20 = 3, so -17 mod 5 = 3. Alternatively, compute |a| mod n, then subtract from n if a was negative.
The modular multiplicative inverse of a modulo n is a number x such that (a × x) mod n = 1. It exists only when a and n are coprime (GCD = 1). For example, the inverse of 3 mod 7 is 5, because (3 × 5) mod 7 = 15 mod 7 = 1.
While modulo is typically defined for integers, some programming languages support floating-point modulo. The result is the signed remainder of the division: 5.5 % 2 = 1.5 in JavaScript. However, this is less common and behavior varies by language.
When you need to wrap around in an array of size n, use index = (index + step) % n. This automatically wraps from the end back to the beginning. For example, in an array of size 10, position 12 wraps to position 2 (12 % 10 = 2).