LCM and GCF Calculator
LCM and GCF of up to 10 numbers with steps: multiples, prime factors, Euclid’s algorithm and a coprime check.
This mod calculator (modulo calculator) finds a mod n, the remainder when a is divided by n, with steps: with .
−17 mod 5
3
In C, Java and JavaScript, −17 % 5 is −2.
−17 mod 5 = 3 in mathematics. Programming languages disagree for negative numbers: Python gives 3, while C, Java and JavaScript give −2.
Divide
Start with the ordinary quotient.
Round the quotient down and find the remainder
The floored convention rounds toward minus infinity, so the remainder takes the sign of n.
Compare the truncated convention
Many programming languages round the quotient toward zero instead, which gives a remainder with the sign of a.
Check
Quotient times divisor plus remainder gives back a.
For example, 17 mod 5 = 2 because 17 = 3 × 5 + 2. It also handles negative numbers, where Python and C or Java disagree, computes power mod by square-and-multiply, and finds modular inverses with the extended Euclidean algorithm.
Division with remainder
Plain-text formula: a = nq + r, q = ⌊a/n⌋, r = a − n⌊a/n⌋
With the quotient rounded down (floored division), the remainder has the same sign as n. For n > 0 this is the usual residue, 0 ≤ r < n. Rounding the quotient toward zero (truncated division) gives a remainder with the sign of a instead.
Square-and-multiply
Plain-text formula: a^b = ∏_{k: b_k = 1} a^2^k, a^2^(k + 1) = (a^2^k)^2
Writing b in binary means about squarings and at most as many multiplications, reducing modulo n each time so the numbers never grow beyond .
Modular inverse
Plain-text formula: ax ≡ 1;(mod ;n) ⇔ ax + ny = 1
An inverse exists exactly when gcd(a, n) = 1. The extended Euclidean algorithm finds x and y, and x reduced modulo n is the inverse.
The big number is a mod n, the remainder in the range 0 to n − 1 for a positive modulus. The quotient and the division statement below it let you check the answer by hand: multiply back and add the remainder.
For positive numbers every convention gives the same remainder. They only differ when a or n is negative, which is why the result lists three remainders. Mathematics, Python and Excel give −17 mod 5 = 3 (count up from −20, the multiple of 5 just below −17). C, C++, Java, JavaScript, C#, Go and Rust compute −17 % 5 = −2, because they round −3.4 toward zero.
A congruence a ≡ r (mod n) means a and r leave the same remainder, or equivalently that n divides a − r. Modular powers are the basis of RSA encryption, Diffie–Hellman key exchange and primality tests; modular inverses are how RSA turns the public exponent into the private one.
Any calculator can find a mod n, even one without a mod key, in three steps:
1. Divide a by n. For 125 mod 7: 125 ÷ 7 = 17.857…
2. Round the quotient down to a whole number: 17. Multiply it back by n: 17 × 7 = 119.
3. Subtract from a: 125 − 119 = 6. So 125 mod 7 = 6, and the formula is .
For a negative a, round down means toward minus infinity, not toward zero. For −17 mod 5: −17 ÷ 5 = −3.4 rounds down to −4, then −17 − 5 × (−4) = −17 + 20 = 3. Dropping the decimals instead (−3) gives −2, the answer C and Java print.
When a is smaller than n the answer is a itself: 1 mod 3 = 1 and 2 mod 4 = 2, because n goes into a zero times. When n divides a evenly the answer is 0: 10 mod 5 = 0. For mod 10 the answer is just the last digit: 1234 mod 10 = 4. In the same way, a number mod 2 is its last binary digit, so it tells you whether the number is odd or even.
Programming languages split into two camps for the % operator. Floored languages give the remainder the sign of n; truncated languages give it the sign of a. The Euclidean remainder is never negative. Here is −7 mod 3 and its relatives under each rule:
To get the mathematical result (0 to n − 1) in a truncated language, write ((a % n) + n) % n, or use Math.floorMod(a, n) in Java and a.rem_euclid(n) in Rust. Python’s math.fmod gives the truncated result if you need it.
| Expression | Floored (Python, Ruby, R %%, Excel MOD) | Truncated (C, C++, Java, JavaScript, C#, Go, Rust %) | Euclidean (math, always ≥ 0) |
|---|---|---|---|
| 7 mod 3 | 1 | 1 | 1 |
| −7 mod 3 | 2 | −1 | 2 |
| 7 mod −3 | −2 | 1 | 1 |
| −7 mod −3 | −1 | −1 | 2 |
| −17 mod 5 | 3 | −2 | 3 |
| −1 mod 26 | 25 | −1 | 25 |
Mod is the arithmetic of anything that wraps around. On a 12-hour clock, 5 hours after 10 o’clock is 3 o’clock, because (10 + 5) mod 12 = 15 mod 12 = 3.
On a 24-hour clock, 30 hours after 22:00 is (22 + 30) mod 24 = 52 mod 24 = 4, so 04:00 two days later.
For weekdays use mod 7. If today is Thursday, then 100 days from now is 100 mod 7 = 2 weekdays later, a Saturday, because 98 days is exactly 14 weeks.
You can reduce each number modulo n before adding, subtracting, multiplying or raising to a power, and the final remainder is the same. That is what keeps modular arithmetic fast. The checks below use n = 7, with 23 ≡ 2 and 19 ≡ 5 (mod 7). Choose a × b mod n above to see the multiplication rule worked step by step.
Division is the exception: to divide by a modulo n, multiply by its modular inverse instead, which exists only when gcd(a, n) = 1. The GCD calculator shows that check with Euclid’s algorithm.
| Rule | Direct | Reduced first |
|---|---|---|
| (a + b) mod n = ((a mod n) + (b mod n)) mod n | 42 mod 7 = 0 | (2 + 5) mod 7 = 0 |
| (a − b) mod n = ((a mod n) − (b mod n)) mod n | 4 mod 7 = 4 | (2 − 5) mod 7 = −3 mod 7 = 4 |
| (a × b) mod n = ((a mod n) × (b mod n)) mod n | 437 mod 7 = 3 | (2 × 5) mod 7 = 10 mod 7 = 3 |
| aᵏ mod n = (a mod n)ᵏ mod n | 23² = 529, 529 mod 7 = 4 | 2² mod 7 = 4 |
A linear congruence such as 3x ≡ 5 (mod 11) is solved by multiplying both sides by the inverse of 3. Choose a⁻¹ mod n above with a = 3 and n = 11 to get 4, since 3 × 4 = 12 ≡ 1. Then x ≡ 4 × 5 = 20 ≡ 9 (mod 11). Check: 3 × 9 = 27 = 2 × 11 + 5.
If g = gcd(a, n) is larger than 1, there are solutions only when g divides b, and then there are g of them. For 6x ≡ 3 (mod 9), g = 3 divides 3, so divide everything by 3 to get 2x ≡ 1 (mod 3), giving x ≡ 2 (mod 3): x = 2, 5 or 8 modulo 9. For 6x ≡ 4 (mod 9) there is no solution, because 3 does not divide 4.
RSA key generation is the same calculation: with public exponent 17 and φ = 3120, the private exponent is the inverse of 17 mod 3120, which is 2753. RSA starts from two large primes, which you can test with the prime number checker.
Answers to values people often look up. Type any of them into the calculator to see the full steps.
| Question | Answer | Why |
|---|---|---|
| 17 mod 3 | 2 | 17 = 5 × 3 + 2 |
| 125 mod 7 | 6 | 125 = 17 × 7 + 6 |
| 1234 mod 10 | 4 | The last digit |
| −17 mod 5 | 3 | −17 = −4 × 5 + 3 |
| 9¹² mod 29 | 24 | Square-and-multiply: 9⁸ × 9⁴ ≡ 20 × 7 |
| 19⁶ mod 23 | 2 | 19 ≡ −4, and 4⁶ = 4096 = 178 × 23 + 2 |
| 5¹⁷ mod 29 | 9 | Square-and-multiply: 5¹⁶ × 5 |
| 3²⁰⁰ mod 13 | 9 | 3³ = 27 ≡ 1, and 200 = 3 × 66 + 2 |
| Inverse of 3 mod 11 | 4 | 3 × 4 = 12 ≡ 1 |
| Inverse of 7 mod 11 | 8 | 7 × 8 = 56 ≡ 1 |
| Inverse of 5 mod 26 | 21 | 5 × 21 = 105 ≡ 1 |
| Inverse of 11 mod 26 | 19 | 11 × 19 = 209 ≡ 1 |
| Inverse of 9 mod 26 | 3 | 9 × 3 = 27 ≡ 1 |
Find the remainder of 3¹³ divided by 7 without computing 3¹³ = 1,594,323 in full.
Answer: . Check: 1,594,323 = 7 × 227,760 + 3.
Divide a by n, round the quotient down to a whole number, multiply it by n and subtract the result from a. For 125 mod 7: 125 ÷ 7 = 17.86, rounded down to 17; 17 × 7 = 119; 125 − 119 = 6.
Use a mod n = a − n × ⌊a ÷ n⌋. Divide, keep the whole-number part, multiply it by n and subtract from a. For 100 mod 7: 100 ÷ 7 = 14.28…, 14 × 7 = 98, 100 − 98 = 2. For a negative a, round the quotient down (−3.4 becomes −4), not toward zero.
a mod n is the remainder after dividing a by n. For example, 125 mod 7 = 6 because 125 = 17 × 7 + 6. Clock arithmetic is mod 12: 5 hours after 10 o’clock is 3 o’clock, since 15 mod 12 = 3.
In mathematics and in Python it is 3, because −17 = 5 × (−4) + 3. In C, Java and JavaScript, −17 % 5 is −2, because those languages use −17 = 5 × (−3) − 2. Both satisfy a = nq + r; they just round the quotient differently.
1. Whenever a is smaller than n (and not negative), a mod n is a itself, because n goes into a zero times: 1 = 0 × 3 + 1. Likewise 2 mod 4 = 2.
0. 5 divides 10 exactly (10 = 2 × 5 + 0), so there is no remainder. a mod n = 0 exactly when n is a factor of a.
As a remainder it is undefined, because it would mean dividing by 0, so the calculator asks for a non-zero modulus. Python raises an error and JavaScript returns NaN; some textbooks define a mod 0 = a as a convention.
Exactly when a and n are coprime (their GCD is 1). For example, 3 × 4 = 12 ≡ 1 (mod 11), so 4 is the inverse of 3 mod 11, but 6 has no inverse mod 9 because gcd(6, 9) = 3.
With square-and-multiply. The work grows with the number of binary digits of b, not with b itself: needs only 9 squarings and 2 multiplications, and a 30-digit exponent needs about 100 squarings.
LCM and GCF of up to 10 numbers with steps: multiples, prime factors, Euclid’s algorithm and a coprime check.
Is it prime? Proof by smallest factor, next and previous primes, and a primes list for any range.
Prime factors with division steps, a factor tree, the divisor count and every divisor.
Binary addition, subtraction and bitwise operations with steps, at 8 to 64 bits, signed or unsigned.
Hex to decimal with place-value steps, plus binary, octal and any base 2 to 36, fractions included.
Exact n! with steps and every digit, plus digit count, trailing zeros, n!! and Stirling’s approximation.
Results are calculated in your browser with methods checked by automated tests, and are intended for learning and general use. For engineering, medical, financial, legal or safety-critical decisions, verify important values independently and follow the rounding rules your work requires. How accurate are these results?