- 2

Hi! I just wrote this Python Script: 3 % 6 and I saw that the answer is 3, it made me wonder why.. Please explain to me..

16th Dec 2021, 12:33 AM
Alvaro Becerra
Alvaro Becerra - avatar
6 Réponses
+ 6
When dividing modulo a smaller number by a larger number, it always results in the smaller number.
16th Dec 2021, 12:42 AM
Solo
Solo - avatar
+ 5
Modulo operator returns the remainder when the two operands are divided together. For 3%6, the quotient would be 0 and remainder 3, hence the result you got via the script.
16th Dec 2021, 2:25 AM
Arsenic
Arsenic - avatar
+ 4
Arsenic, please explain in more detail how you perform calculations ☺️
16th Dec 2021, 7:49 AM
Solo
Solo - avatar
+ 3
Vasiliy it's fundamental Euclidean division, I don't think it gets simpler than this. But since you asked, let me try to explain it even more. According to Euclid's division lemma, For any given 2 *integers* ( let's call them A and B ), there exist 2 unique *integers* Q ( called quotient ) and R ( called Remainder ) such that A = (B * Q) + R { considering 0 <= R<= |B| ,where |B| is absolute value of integer B } By definition, when we perform a modulo operation (%) between A and B, it returns it's remainder (R) In ValAlv 's case, A = 3 and B = 6 so the equation looks something like this 3 = ( Q * 6 ) + R If we put the value of Q = 0 in this case then the equation becomes 3 = ( 0 * 6 ) + R 3 = R Which means the only possible value of R in this case is 3 which is actually what the OP's script correctly return. I hope it's clear now. If you still have any doubts regarding any step then feel free to ask. 👍
16th Dec 2021, 12:43 PM
Arsenic
Arsenic - avatar
+ 3
Arsenic, 😳 you are familiar with wikipedia 😃, only one question remains. Why make a calculation if it is initially known that the dividend is less than the divisor? 😊
16th Dec 2021, 2:18 PM
Solo
Solo - avatar
+ 2
Thanks. 。◠‿◠。
16th Dec 2021, 12:53 AM
Alvaro Becerra
Alvaro Becerra - avatar