0
0
0
0
0
0
0
0
The modulo operation (often abbreviated as mod) finds the remainder after dividing one integer by another. While it may seem like a simple extension of division, the modulo operation is one of the most powerful and widely used concepts in mathematics and computer science. Our Modulo Calculator computes both the JavaScript-style remainder (which preserves the sign of the dividend) and the Euclidean modulo (which is always non-negative), giving you flexibility for any application.
The modulo operation is defined as: given two numbers a (the dividend) and b (the modulus), the result of a mod b is the remainder when a is divided by b. Formally: $$a \mod b = a - b \cdot \lfloor a/b \rfloor$$. For positive numbers, this is straightforward: 17 mod 5 = 2, because 17 = 5 x 3 + 2. However, when negative numbers are involved, different programming languages handle the operation differently, which is why this calculator provides both variants.
In modular arithmetic, we focus on remainders rather than exact values. Two integers are said to be congruent modulo n (written as a ≡ b (mod n)) if they have the same remainder when divided by n. This concept, formalized by Carl Friedrich Gauss in his 1801 masterwork Disquisitiones Arithmeticae, underpins modern cryptography, coding theory, and abstract algebra.
The modulo operation appears in numerous practical applications. In computer science, it is used for hash functions, circular buffers, and array index wrapping. In cryptography, modular exponentiation is the foundation of RSA encryption and Diffie-Hellman key exchange. In everyday life, clock arithmetic is modular: 14:00 is the same as 2:00 PM because 14 mod 12 = 2. Days of the week cycle with period 7, so determining what day it will be 100 days from Monday is a modulo problem: (1 + 100) mod 7 = 3, which corresponds to Wednesday.
A subtle but important distinction exists between different definitions of the modulo operation for negative numbers. The truncated modulo (used by JavaScript's % operator, C, C++, and Java) preserves the sign of the dividend: -7 % 3 = -1. The Euclidean modulo always returns a non-negative result: -7 mod 3 = 2 (because -7 = 3 x (-3) + 2). The floored modulo (used by Python) preserves the sign of the divisor. Our calculator shows both the truncated and Euclidean versions so you can use whichever convention your application requires.
Key properties of the modulo operation include: (a + b) mod n = ((a mod n) + (b mod n)) mod n, and (a x b) mod n = ((a mod n) x (b mod n)) mod n. These properties allow computations with very large numbers by reducing intermediate results, which is essential in cryptographic algorithms where numbers may have hundreds of digits.
The Modulo Calculator computes the following:
JavaScript Remainder (%): $$a \% b = a - b \cdot \text{trunc}(a/b)$$
This preserves the sign of the dividend. For example, -7 % 3 = -1.
Euclidean Modulo: $$a \bmod b = ((a \% b) + |b|) \% |b|$$
This always returns a non-negative result in the range [0, |b|). For example, -7 mod 3 = 2.
Integer Quotient: The truncated quotient $$q = \text{trunc}(a/b)$$ used in the division algorithm.
Congruence Class: The Euclidean modulo value, representing the equivalence class of a in the integers modulo b.
The JavaScript % result is what most programming languages return for the % operator. If you are debugging code, this is the value your program produces. The Euclidean Modulo is the mathematically standard remainder, always in the range [0, |b|). This is what mathematicians typically mean by mod. The Integer Quotient shows how many whole times b fits into a (truncated toward zero). The Congruence Class identifies which residue class a belongs to modulo b, useful in number theory and cryptography.
If both inputs are positive, all remainder variants agree. Differences appear only when a is negative. Choose the variant that matches your application: JavaScript/C-style for programming, Euclidean for mathematics.
Inputs
Results
17 mod 5 = 2, because 17 = 5 x 3 + 2. Both the JavaScript % and Euclidean modulo give 2 since the inputs are positive.
Inputs
Results
-7 % 3 = -1 (JavaScript style, sign of dividend preserved). Euclidean: -7 mod 3 = 2, because -7 = 3 x (-3) + 2. Note the different results for negative inputs.
The modulo operation (a mod b) computes the remainder after dividing a by b. For example, 17 mod 5 = 2 because 17 divided by 5 gives quotient 3 with remainder 2. It is one of the most fundamental operations in number theory and computer science.
For positive numbers, modulo and remainder are identical. The difference appears with negative numbers. The remainder (as computed by most programming languages' % operator) preserves the sign of the dividend: -7 % 3 = -1. The Euclidean modulo always returns a non-negative result: -7 mod 3 = 2. The choice depends on convention and application.
Different systems define it differently. Truncated division (C, Java, JavaScript %): the remainder has the sign of the dividend. Floored division (Python %): the remainder has the sign of the divisor. Euclidean: the remainder is always non-negative. For example, -7 mod 3 equals -1 (truncated), 2 (floored/Euclidean).
Modular arithmetic is a system where numbers 'wrap around' after reaching a certain value (the modulus). Two numbers are congruent modulo n if they differ by a multiple of n. Clock arithmetic is a familiar example: 15:00 and 3:00 are congruent modulo 12. Gauss formalized this in 1801, and it is now foundational in cryptography and algebra.
Common uses include: checking if a number is even or odd (n % 2), implementing circular arrays (index % length), computing hash values, generating cyclic patterns, checking divisibility, and performing clock arithmetic for time calculations.
Modular arithmetic is the backbone of modern cryptography. RSA encryption uses modular exponentiation: c = m^e mod n. Diffie-Hellman key exchange relies on the difficulty of the discrete logarithm problem in modular arithmetic. These systems work because modular exponentiation is fast to compute but extremely hard to reverse without the private key.
Key properties include: (1) (a + b) mod n = ((a mod n) + (b mod n)) mod n, (2) (a x b) mod n = ((a mod n) x (b mod n)) mod n, (3) (a - b) mod n = ((a mod n) - (b mod n) + n) mod n. These properties allow modular reduction at each step of a computation, keeping numbers small.
A congruence class modulo n is the set of all integers that have the same remainder when divided by n. For example, the congruence class of 2 modulo 5 is {..., -8, -3, 2, 7, 12, 17, ...}. There are exactly n congruence classes modulo n, labeled 0 through n-1, and they partition the integers into n groups.
The square-and-multiply algorithm (also called binary exponentiation) computes a^b mod n efficiently by repeatedly squaring and reducing modulo n. It runs in O(log b) multiplications instead of O(b), making it feasible for the enormous exponents used in cryptography (hundreds of digits).
The Chinese Remainder Theorem (CRT) states that if you know the remainders of a number when divided by several pairwise coprime moduli, you can uniquely reconstruct the number modulo the product of those moduli. For example, if x mod 3 = 2 and x mod 5 = 3, then x mod 15 = 8. CRT is used to speed up RSA computations and solve systems of congruences.
Roboculator Team
The Roboculator Team explains calculations, planning tools, and practical formulas in clear language for real-life situations.
How helpful was this calculator?
Be the first to rate!