Advertisement

Is modulo associative?

Modular addition and multiplication obey familiar algebraic laws for a fixed modulus. The binary remainder operation is different. Compare both groupings with three integers.

Calculator

Three-term modular sum
2.00000000
Three-factor modular product
4.00000000
(a mod b) mod c
2.00000000
a mod (b mod c)
0.00000000
Compare the two sides
Law or comparisonLeft sideRight side
Addition: associative2.000000002.00000000
Multiplication: associative4.000000004.00000000
Addition: commutative5.000000005.00000000
Multiplication: commutative1.000000001.00000000
Multiplication distributes over addition4.000000004.00000000
Remainder operator: grouping comparison2.000000000.00000000

Advertisement

How this calculator works

The fixed modulus m defines residue classes. Reducing after each addition or multiplication gives the same residue as reducing at the end, so these operations are associative and commutative, and multiplication distributes over addition. In contrast, (a mod b) mod c changes the modulus at each step. It need not equal a mod (b mod c). One counterexample disproves a universal law; agreement for one triple does not prove one. b and c must be positive here so both nested remainder expressions use the same non-negative convention.

Formula

((a+b) mod m+c) mod m=(a+(b+c) mod m) mod m. The analogous product identity holds. (a mod b) mod c need not equal a mod (b mod c).

Worked example

For a=20, b=6 and c=4, (20 mod 6) mod 4=2, while 20 mod (6 mod 4)=0. With fixed modulus 7, the three-term sum is 2 and the three-factor product is 4 under either grouping.

Advertisement

Frequently asked questions

Is the remainder operator associative?

Not in general. The two nested remainder columns can differ, as the default example shows.

Why do modular addition and multiplication work?

They use one fixed modulus throughout. Reducing an intermediate result changes it by a multiple of that same modulus.

Why is b mod c=0 rejected?

It becomes the divisor in the right-hand nesting. Division or remainder by zero is undefined.

Sources

Advertisement