Modular Arithmetic

15≡−9(mod12).
I am not able to understand this.How does -9 when divided by 12 leaves 3 as remainder.
https://www.geeksforgeeks.org/modulus-on-negative-numbers/ according to this page
when -9 divided by 12 remainder is -9.

Because if you subtract 3 from -9, the number will become -12 which is divisible by 12

1 Like

For C++
-9 % 12
>> -9
This will essentially result in -9 for C++. The article is right. To calculate mod with negative numbers the mod is first calculated from their absolute values and then the sign of the first number is given to the result.

But in python the result is given the sign of the denominator.
-9 % 12
>> 3
First we get the floor value of numerator / denominator :
floor(-9 / 12) => -1
Now we apply the basic theory of division : Numerator = Quotient * Denominator + Remainder
-9 = -1 * 12 + R
R = 3

1 Like