Negative module | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Negative module

-5 % 3 = (1 -2*3) % 3 = 1 What is 1 inside bracket i mean where it comes from ?

13th Jan 2021, 6:01 AM
Gautam Yadav
Gautam Yadav - avatar
4 Answers
+ 4
Adding to Ipang 's answer If read the article again, it is written (a + b) % n = [(a % n) + (b % n)] % n that is why -5 % 3 has been written as (1-2*3) % 3 because (1 - 2*3) % 3 = [(1 % 3) - {(2 * 3) % 3}] = [(1 % 3) - 0] = 1
13th Jan 2021, 6:43 AM
XXX
XXX - avatar
+ 3
I think you need to show full version of the code, as it is, it doesn't make sense. Where you see this?
13th Jan 2021, 6:13 AM
Ipang
+ 2
13th Jan 2021, 6:16 AM
Gautam Yadav
Gautam Yadav - avatar
+ 2
Gautam Yadav It's ok, maybe now I see a bit clearer I think maybe it is just an illustration (I could be wrong though), 1 - 2 * 3 equals to 1 - 6 which is -5 Thus (-5 % 3) and ((1 - 2 * 3) % 3) will give same result -> 1. And here are some search result links to discussions about modulo operation with negative operand https://www.sololearn.com/Discuss/2565566/?ref=app https://www.sololearn.com/Discuss/2203461/?ref=app
13th Jan 2021, 6:40 AM
Ipang