Is Modulo Associative? Calculator

Demonstrate whether the modulo operation is associative by comparing (a mod b) mod c with a mod (b mod c).

Enter Values

Enter three integers a, b, c to test if (a mod b) mod c equals a mod (b mod c).

Result

Are they equal?
NOT EQUAL
Modulo is NOT associative
(a mod b) mod c --
a mod (b mod c) --
a mod b --
b mod c --

Step-by-Step Computation

(a mod b) mod c vs a mod (b mod c)

Is the Modulo Operation Associative?

No, the modulo operation is generally NOT associative. An operation * is associative if (a * b) * c = a * (b * c) for all valid inputs. The modulo operation fails this test for most combinations of integers, meaning (a mod b) mod c is usually not equal to a mod (b mod c).

Understanding the Problem

Left-Associative: (a mod b) mod c

First compute a mod b, then take the result modulo c.

(17 mod 5) mod 3 = 2 mod 3 = 2

Right-Associative: a mod (b mod c)

First compute b mod c, then compute a modulo that result.

17 mod (5 mod 3) = 17 mod 2 = 1

The Verdict

Since 2 != 1, modulo is not associative. Try different values to see for yourself.

(a mod b) mod c != a mod (b mod c)

Why Modulo Is Not Associative

The modulo operation computes the remainder after division. The order of operations matters because the intermediate result changes the divisor in the second operation. When you compute (a mod b) first, the result is at most b-1, which then gets reduced by mod c. Conversely, when you compute (b mod c) first, you create a potentially much smaller divisor for a, which can produce a very different remainder.

When Are They Equal?

While modulo is not generally associative, there are specific cases where the results happen to coincide:

  • When a < b and a < (b mod c): both sides equal a mod (b mod c).
  • When c divides b evenly: b mod c = 0, making the right side undefined (division by zero).
  • When a mod b < b mod c: the left side might equal the right side by coincidence.
  • Certain symmetric cases where a, b, c have special relationships.

Other Properties of Modulo

  • Commutative? No. a mod b != b mod a in general (e.g., 7 mod 3 = 1, but 3 mod 7 = 3).
  • Distributive over addition? Yes! (a + b) mod c = ((a mod c) + (b mod c)) mod c.
  • Distributive over multiplication? Yes! (a * b) mod c = ((a mod c) * (b mod c)) mod c.
  • Identity element? No universal identity element exists for modulo.

Practical Implications

In programming, the non-associativity of modulo means you must be careful about operator precedence and parenthesization. Expression a % b % c is evaluated left-to-right as (a % b) % c in most languages (C, Java, Python, JavaScript), but this is different from a % (b % c). Always use explicit parentheses to make your intent clear.

How to Use This Calculator

  1. Enter three integer values a, b, and c.
  2. Click "Check Associativity" to compare both groupings.
  3. Click "Find Counterexamples" to generate multiple examples showing non-associativity.