为什么 2 mod 4 = 2?
我都不好意思问这么简单的问题。我的学期还有两周才开始,所以我不能问教授,这种悬念会杀了我。
为什么 2 mod 4 = 2?
I'm embarrassed to ask such a simple question. My term does not start for two more weeks so I can't ask a professor, and the suspense would kill me.
Why does 2 mod 4 = 2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
Mod 只是意味着执行除法后取余数。由于 4 能除以 2 个零,因此余数为 2。
Mod just means you take the remainder after performing the division. Since 4 goes into 2 zero times, you end up with a remainder of 2.
模是余数,而不是除法。
符号
%
通常用于模运算符,代替单词mod
。对于
x % 4
,您将得到下表(适用于 1-10)Modulo is the remainder, not division.
The sign
%
is often used for the modulo operator, in lieu of the wordmod
.For
x % 4
, you get the following table (for 1-10)Modulo (mod, %) 是求余运算符。
Modulo (mod, %) is the Remainder operator.
如果你用香蕉和一群人的话会容易得多。
假设您有 1 个香蕉和 6 个人,您可以这样表达:
1 mod 6
/1 % 6
/1 modulo 6
。团队中每人需要 6 根香蕉才能吃饱并快乐。
因此,如果您有 1 根香蕉,需要与 6 个人分享,但只有当您为每个小组成员(即 6 人)拥有 1 根香蕉时,您才能分享,那么您将拥有 1 根香蕉(剩余的,不与任何人分享)组),同样适用于 2 个香蕉。然后你将得到 2 根香蕉作为剩余部分(没有任何东西是共享的)。
但是当你得到 6 根香蕉时,那么你应该很高兴,因为这样 6 人一组中每个成员就有 1 根香蕉,而当你将所有 6 根香蕉分享给 6 个人时,剩下的就是 0 根香蕉或者没有香蕉了。
现在,对于 7 根香蕉和 6 人一组,您将得到
7 mod 6 = 1
,这是因为您给了 6 个人每人 1 根香蕉,剩下的就是 1 根香蕉。对于
12 mod 6
或 6 个人分享 12 个香蕉,每人将有两个香蕉,余数为 0。Much easier if u use bananas and a group of people.
Say you have 1 banana and group of 6 people, this you would express:
1 mod 6
/1 % 6
/1 modulo 6
.You need 6 bananas for each person in group to be well fed and happy.
So if you then have 1 banana and need to share it with 6 people, but you can only share if you have 1 banana for each group member, that is 6 persons, then you will have 1 banana (remainder, not shared on anyone in group), the same goes for 2 bananas. Then you will have 2 banana as remainder (nothing is shared).
But when you get 6 bananas, then you should be happy, because then there is 1 banana for each member in group of 6 people, and the remainder is 0 or no bananas left when you shared all 6 bananas on 6 people.
Now, for 7 bananas and 6 people in group, you then will have
7 mod 6 = 1
, this because you gave 6 people 1 banana each, and 1 banana is the remainder.For
12 mod 6
or 12 bananas shared on 6 people, each one will have two bananas, and the remainder is then 0.2 / 4 = 0 余数为 2
2 / 4 = 0 with a remainder of 2
就在几分钟前,我也对此感到困惑。然后我在一张纸上手工进行了除法,结果很有意义:
这就是计算机将要解决的问题。计算机停在那里并返回 2,这是有道理的,因为这就是“%”(mod)所要求的。
我们受过训练,要输入小数并继续下去,这就是为什么这一开始可能是违反直觉的。
I was confused about this, too, only a few minutes ago. Then I did the division long-hand on a piece of paper and it made sense:
That's as far as the computer is going to take this problem. The computer stops there and returns the 2, which makes sense since that's what "%" (mod) is asking for.
We've been trained to put in the decimal and keep going which is why this can be counterintuitive at first.
有人联系我并要求我在问题评论中更详细地解释我的答案。所以这是我回复那个人的内容,以防对其他人有所帮助:
Someone contacted me and asked me to explain in more details my answer in the comment of the question. So here is what I replied to that person in case it can help anyone else:
mod表示除以时的余数。所以2除以4就是0,还剩下2。因此 2 mod 4 是 2。
mod means the reaminder when divided by. So 2 divided by 4 is 0 with 2 remaining. Therefore 2 mod 4 is 2.
模运算符计算两个整数操作数相除的余数。以下是一些示例:
The modulo operator evaluates to the remainder of the division of the two integer operands. Here are a few examples:
模是数学除法表达式的余数,以整数表示。
所以,假设屏幕上有一个像素位于位置 90,屏幕宽度为 100 像素,再加上 20,它将环绕到位置 10。为什么...因为 90 + 20 = 110,因此 110 % 100 = 10。
为了理解它,我认为模是小数的整数表示。此外,如果您向后执行表达式并将余数处理为小数,然后添加到除数,它将给您原始答案。
示例:
逆向工程为:
Modulo is the remainder, expressed as an integer, of a mathematical division expression.
So, lets say you have a pixel on a screen at position 90 where the screen is 100 pixels wide and add 20, it will wrap around to position 10. Why...because 90 + 20 = 110 therefore 110 % 100 = 10.
For me to understand it I consider the modulo is the integer representation of fractional number. Furthermore if you do the expression backwards and process the remainder as a fractional number and then added to the divisor it will give you your original answer.
Examples:
Reversed engineered to:
当你用 2 除以 4 时,你会得到 0,剩下 2。模只是数字相除后的余数。
When you divide 2 by 4, you get 0 with 2 left over or remaining. Modulo is just the remainder after dividing the number.
我认为您对如何读取模方程感到困惑。
当我们编写诸如
2/4
之类的除法方程时,我们将 2 除以 4。当编写诸如
2 % 4
之类的模方程时,我们将2 除除以 4
(认为 2 除 4)并返回余数。I think you are getting confused over how the modulo equation is read.
When we write a division equation such as
2/4
we are dividing 2 by 4.When a modulo equation is wrote such as
2 % 4
we are dividing2 by 4
(think 2 over 4) and returning the remainder.MOD 是余数运算符。这就是为什么 2 mod 4 得到 2 作为余数。 4*0=0,然后2-0=2。为了使它更清楚,请尝试对 6 mod 4 或 8 mod 3 执行相同的操作。
MOD is remainder operator. That is why 2 mod 4 gives 2 as remainder. 4*0=0 and then 2-0=2. To make it more clear try to do same with 6 mod 4 or 8 mod 3.
这就是欧几里得算法。
例如
a mod b = k * b + c => a mod b = c,其中 k 是整数,c 是答案
4 mod 2 = 2 * 2 + 0 => 4 mod 2 = 0
27 mod 5 = 5 * 5 + 2 => 27 mod 5 = 2
所以你的答案是
2 mod 4 = 0 * 4 + 2 => 2 模 4 = 2
This is Euclid Algorithm.
e.g
a mod b = k * b + c => a mod b = c, where k is an integer and c is the answer
4 mod 2 = 2 * 2 + 0 => 4 mod 2 = 0
27 mod 5 = 5 * 5 + 2 => 27 mod 5 = 2
so your answer is
2 mod 4 = 0 * 4 + 2 => 2 mod 4 = 2
对于:
2 mod 4
我们可以使用我想出的这个小公式,也许它已经在我不知道的地方定义了,但对我有用,而且它真的很有用。
A mod B = C
其中 C 是答案K * B - A = |C|
其中 K 是 B 适合 A 的次数2 mod 4< /code> 将为:
0 * 4 - 2 = |C|
C = |-2| => 2
希望它对您有用:)
For:
2 mod 4
We can use this little formula I came up with after thinking a bit, maybe it's already defined somewhere I don't know but works for me, and its really useful.
A mod B = C
where C is the answerK * B - A = |C|
where K is how many times B fits in A2 mod 4
would be:0 * 4 - 2 = |C|
C = |-2| => 2
Hope it works for you :)
Mod操作与提醒一起使用。
这称为模运算。
Mod operation works with reminder.
This is called modular arithmetic.
要回答模
x % y
,您需要问两个问题:A-
y
进入x
中有多少次没有余数?对于 2%4,则为 0。B-您需要添加多少才能从该值返回到
x
?要从 0 回到 2,您需要 2-0,即 2。这些可以总结为一个问题,如下所示:
您需要在
x
除以y
的整数结果上添加多少,才能得到x
?整数意味着只有整数而不是任何感兴趣的分数。
小数除法余数(例如 0.283849)对模数不感兴趣,因为模数仅处理整数。
To answer a modulo
x % y
, you ask two questions:A- How many times
y
goes inx
without remainder ? For 2%4 that's 0.B- How much do you need to add to get from that back to
x
? To get from 0 back to 2 you'll need 2-0, i.e. 2.These can be summed up in one question like so:
How much will you need to add to the integer-ish result of the division of
x
byy
, to get back atx
?By integer-ish it is meant only whole numbers and not fractions whatsoever are of interest.
A fractional division remainder (e.g. .283849) is not of interest in modulo because modulo only deals with integer numbers.
为了以直观的方式思考这个问题,请想象一个钟面,在您的特定示例中,该钟面仅转到 4 而不是 12。如果您从时钟上的 4 开始(就像从 0 开始)并顺时针旋转一周2“小时”,你会落在 2 上,就像顺时针绕它 6“小时”一样,你也会落在 2 上(6 mod 4 == 2 就像 2 mod 4 == 2 一样)。
For a visual way to think about it, picture a clock face that, in your particular example, only goes to 4 instead of 12. If you start at 4 on the clock (which is like starting at zero) and go around it clockwise for 2 "hours", you land on 2, just like going around it clockwise for 6 "hours" would also land you on 2 (6 mod 4 == 2 just like 2 mod 4 == 2).
现在可能是提及 modr() 函数的好时机。它返回除法的整个部分和剩余部分。
This could be a good time to mention the modr() function. It returns both the whole and the remainder parts of a division.
我的做法是,2%4 可以解释为小于或等于 2 的 4 的最大因数,即 0,因此 2(2%4 的左操作数)减(-) 0 是 2
The way I go about it is, 2%4 can be interpreted as what is the highest factor of 4 that is less or equal to 2, and that is 0, therefore 2 (the left operand from 2%4) minus(-) 0 is 2
模法
首先需要将股息除以除数:
2
4
= 0.50
接下来,我们取商 (0) 的整数部分,并将其乘以除数 (4):
0 x 4 = 0
最后,我们取第二步中的答案,然后从被除数中减去它,得到 2 的答案mod 4:
2 - 0 = 2
如您所见,2 mod 4 的答案是 2。
Modulo Method
First need to divide the Dividend by the Divisor:
2
4
= 0.50
Next we take the Whole part of the Quotient (0) and multiply that by the Divisor (4):
0 x 4 = 0
And finally, we take the answer in the second step and subtract it from the Dividend to get the answer to 2 mod 4:
2 - 0 = 2
As you can see, the answer to 2 mod 4 is 2.