Math Fundamentals18 sections · 814 units
Open in Course

Why Properties Matter

Preventing overflow

If you need to compute 123456789×987654321mod1000000007123456789 \times 987654321 \bmod 1000000007, multiplying first might overflow your integer type.

Instead, you can reduce both numbers modulo 10000000071000000007 first, multiply the smaller results, then take modulo again. The answer stays the same, but your numbers stay within bounds.

This pattern appears in almost every problem that asks for an answer modulo some large prime. Without it, your code crashes or gives wrong answers.