为什么 2 mod 4 = 2?

发布于 2024-08-03 19:02:55 字数 77 浏览 3 评论 0原文

我都不好意思问这么简单的问题。我的学期还有两周才开始,所以我不能问教授,这种悬念会杀了我。

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

岁月苍老的讽刺 2024-08-10 19:02:55

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.

往事风中埋 2024-08-10 19:02:55

是余数,而不是除法。

2 / 4 = 0R2
2 % 4 = 2

符号 % 通常用于模运算符,代替单词 mod

对于 x % 4,您将得到下表(适用于 1-10)

 x x%4
------
 1  1
 2  2
 3  3
 4  0
 5  1
 6  2
 7  3
 8  0
 9  1
10  2

Modulo is the remainder, not division.

2 / 4 = 0R2
2 % 4 = 2

The sign % is often used for the modulo operator, in lieu of the word mod.

For x % 4, you get the following table (for 1-10)

 x x%4
------
 1  1
 2  2
 3  3
 4  0
 5  1
 6  2
 7  3
 8  0
 9  1
10  2
热血少△年 2024-08-10 19:02:55

Modulo (mod, %) 是求余运算符。

2%2 = 0 (2/2 = 1 remainder 0)
1%2 = 1 (1/2 = 0 remainder 1)
4%2 = 0 (4/2 = 2 remainder 0)
5%2 = 1 (5/2 = 2 remainder 1)

Modulo (mod, %) is the Remainder operator.

2%2 = 0 (2/2 = 1 remainder 0)
1%2 = 1 (1/2 = 0 remainder 1)
4%2 = 0 (4/2 = 2 remainder 0)
5%2 = 1 (5/2 = 2 remainder 1)
陌生 2024-08-10 19:02:55

如果你用香蕉和一群人的话会容易得多。

假设您有 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.

说不完的你爱 2024-08-10 19:02:55

2 / 4 = 0 余数为 2

2 / 4 = 0 with a remainder of 2

_畞蕅 2024-08-10 19:02:55

就在几分钟前,我也对此感到困惑。然后我在一张纸上手工进行了除法,结果很有意义:

  • 4 可以除以 2 个零。
  • 4 乘以 0 等于 0。
  • 你把 0 放在 2 的下面,然后减去,剩下 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:

  • 4 goes into 2 zero times.
  • 4 times 0 is 0.
  • You put that zero under the 2 and subtract which leaves 2.

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.

征棹 2024-08-10 19:02:55

有人联系我并要求我在问题评论中更详细地解释我的答案。所以这是我回复那个人的内容,以防对其他人有所帮助:

模运算给出欧几里德除法的余数
(仅适用于整数,不适用于实数)。如果你有一个这样的
A = B * C + D(其中 D < B),则 A 的欧几里得除法的商
除以 B 为 C,余数为 D。如果将 2 除以 4,则商
是0,余数是2。

假设您有 A 对象(无法剪切)。而你想要
将相同数量的这些物品分发给 B 人。只要
你有多个 B 对象,你给每个对象 1,然后重复。
当你剩下的物品少于 B 个时,你会停下来并保留剩余的物品
对象。您重复该操作的次数,让我们
称这个数字为C,是商。您保留的物品数量
最后,我们称之为 D,是余数。

如果你有 2 个物体和 4 个人。您已经少于 4 个
对象。所以每个人得到 0 个物体,你保留 2 个。

这就是为什么 2 模 4 等于 2。

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:

The modulo operation gives you the remainder of the euclidian disivion
(which only works with integer, not real numbers). If you have A such
that A = B * C + D (with D < B), then the quotient of the euclidian division of A
by B is C, and the remainder is D. If you divide 2 by 4, the quotient
is 0 and the remainder is 2.

Suppose you have A objects (that you can't cut). And you want to
distribute the same amount of those objects to B people. As long as
you have more than B objects, you give each of them 1, and repeat.
When you have less than B objects left you stop and keep the remaining
objects. The number of time you have repeated the operation, let's
call that number C, is the quotient. The number of objects you keep at
the end, let's call it D, is the remainder.

If you have 2 objects and 4 people. You already have less than 4
objects. So each person get 0 objects, and you keep 2.

That's why 2 modulo 4 is 2.

梦醒时光 2024-08-10 19:02:55

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.

云归处 2024-08-10 19:02:55

模运算符计算两个整数操作数相除的余数。以下是一些示例:

23 % 10 evaluates to 3 (because 23/10 is 2 with a remainder of 3)
50 % 50 evaluates to 0 (50/50 is 1 with a remainder of 0)
9 % 100 evaluates to 9 (9/100 is 0 with a remainder of 9)

The modulo operator evaluates to the remainder of the division of the two integer operands. Here are a few examples:

23 % 10 evaluates to 3 (because 23/10 is 2 with a remainder of 3)
50 % 50 evaluates to 0 (50/50 is 1 with a remainder of 0)
9 % 100 evaluates to 9 (9/100 is 0 with a remainder of 9)
流星番茄 2024-08-10 19:02:55

模是数学除法表达式的余数,以整数表示。

所以,假设屏幕上有一个像素位于位置 90,屏幕宽度为 100 像素,再加上 20,它将环绕到位置 10。为什么...因为 90 + 20 = 110,因此 110 % 100 = 10。

为了理解它,我认为模是小数的整数表示。此外,如果您向后执行表达式并将余数处理为小数,然后添加到除数,它将给您原始答案。

示例:

    100
(A) ---  =  14 mod 2
     7

    123
(B) ---  =  8 mod 3
     15

     3
(C) ---  =  0 mod 3
     4

逆向工程为:

                        2      14(7)    2       98    2     100
 (A) 14 mod 2  =  14 + ---  =  ----- + ---  =  --- + ---  = ---
                        7        7      7       7     7      7

                      3      8(15)    3      120    3      123
 (B) 8 mod 3  =  8 + ---  =  ----- + ---  =  --- + ---  =  ---
                      15       15     15      15    15      15

                      3       3
 (B) 0 mod 3  =  0 + ---  =  ---
                      4       4

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:

    100
(A) ---  =  14 mod 2
     7

    123
(B) ---  =  8 mod 3
     15

     3
(C) ---  =  0 mod 3
     4

Reversed engineered to:

                        2      14(7)    2       98    2     100
 (A) 14 mod 2  =  14 + ---  =  ----- + ---  =  --- + ---  = ---
                        7        7      7       7     7      7

                      3      8(15)    3      120    3      123
 (B) 8 mod 3  =  8 + ---  =  ----- + ---  =  --- + ---  =  ---
                      15       15     15      15    15      15

                      3       3
 (B) 0 mod 3  =  0 + ---  =  ---
                      4       4
心安伴我暖 2024-08-10 19:02:55

当你用 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.

箜明 2024-08-10 19:02:55

我认为您对如何读取模方程感到困惑。

当我们编写诸如 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 dividing 2 by 4 (think 2 over 4) and returning the remainder.

夏有森光若流苏 2024-08-10 19:02:55

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.

沉默的熊 2024-08-10 19:02:55

这就是欧几里得算法。

例如

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

病毒体 2024-08-10 19:02:55

对于:

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 answer

K * B - A = |C| where K is how many times B fits in A

2 mod 4 would be:

0 * 4 - 2 = |C|

C = |-2| => 2

Hope it works for you :)

一袭水袖舞倾城 2024-08-10 19:02:55

Mod操作与提醒一起使用。

这称为模运算。

 a==b(mod m) 
 then m|(a-b)
 a-b=km 
 a=b+km
 So, 2=2+0*4

Mod operation works with reminder.

This is called modular arithmetic.

 a==b(mod m) 
 then m|(a-b)
 a-b=km 
 a=b+km
 So, 2=2+0*4
飞烟轻若梦 2024-08-10 19:02:55

要回答模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 in x 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 by y, to get back at x?

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.

街道布景 2024-08-10 19:02:55

为了以直观的方式思考这个问题,请想象一个钟面,在您的特定示例中,该钟面仅转到 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).

堇年纸鸢 2024-08-10 19:02:55

现在可能是提及 modr() 函数的好时机。它返回除法的整个部分和剩余部分。

print("\n 17 // 3 =",17//3," # Does the same thing as int(17/3)")
print(" 17 %  3 =",17%3," # Modulo division gives the remainder.")
whole, remain = divmod(17,3)
print(" divmod(17,3) returns ->",divmod(17,3),end="")
print(" because 3 goes into 17,",whole,"times with a remainder of",remain,end=".\n\n")

This could be a good time to mention the modr() function. It returns both the whole and the remainder parts of a division.

print("\n 17 // 3 =",17//3," # Does the same thing as int(17/3)")
print(" 17 %  3 =",17%3," # Modulo division gives the remainder.")
whole, remain = divmod(17,3)
print(" divmod(17,3) returns ->",divmod(17,3),end="")
print(" because 3 goes into 17,",whole,"times with a remainder of",remain,end=".\n\n")
土豪我们做朋友吧 2024-08-10 19:02:55

我的做法是,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

不交电费瞎发啥光 2024-08-10 19:02:55

模法
首先需要将股息除以除数:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文