왜 2 mod 4 = 2입니까?
나는 그런 간단한 질문을하기가 부끄럽다. 내 임기는 2 주 동안 더 시작되지 않아 교수에게 물어볼 수없고 긴장은 나를 죽일 것이다.
왜 2 mod 4 = 2입니까?
Mod는 단지 나눗셈을 한 후에 나머지를 취하는 것을 의미합니다. 4는 2 번 0으로 들어가므로 나머지는 2로 끝납니다.
모듈 로는 나머지가 아니라 나눗셈입니다.
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
모듈로 (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)
바나나와 사람들을 사용하면 훨씬 쉽습니다.
바나나 1 개와 6 명으로 구성된 그룹이 있다고 가정하면 다음과 같이 표현할 수 있습니다. 1 mod 6
/ 1 % 6
/ 1 modulo 6
.
잘 지내고 행복하게 지내려면 그룹의 각 사람마다 6 개의 바나나가 필요합니다.
따라서 바나나 1 개를 가지고 있고 6 명과 공유해야하지만 각 그룹 구성 원당 바나나 1 개, 즉 6 명인 경우에만 공유 할 수 있습니다. 그러면 바나나 1 개 (나머지, 2) 바나나도 마찬가지입니다. 그러면 2 개의 바나나가 남게됩니다 (아무것도 공유하지 않습니다).
그러나 6 개의 바나나를 받으면 6 명으로 구성된 그룹의 각 구성원에 대해 1 개의 바나나가 있고 나머지 6 명이 바나나를 6 명이 공유 할 때 남은 바나나가 없기 때문에 행복해야합니다.
이제 바나나 7 개와 그룹 6 명에 7 mod 6 = 1
대해 6 명씩 바나나 1 개를 주었고 나머지는 바나나 1 개가 되었기 때문에 이렇게됩니다.
들어 12 mod 6
육명에서 공유 바나나 또는 12, 각각 두 개의 바나나를 가질 것이며, 나머지는 0이다.
2의 나머지는 2/4 = 0
불과 몇 분 전에 이것에 대해서도 혼란 스러웠습니다. 그런 다음 한 장의 종이에 오랫동안 손을 댔습니다.
- 4는 2 번 0으로 들어갑니다
- 4 곱하기 0은 0입니다.
- 2 아래에 0을 넣고 2를 빼면 2가 남습니다.
그것은 컴퓨터가이 문제를 겪는 한입니다. 컴퓨터는 거기서 멈추고 2를 반환하는데, 이는 "%"(mod)가 요구하는 것이므로 의미가 있습니다.
우리는 십진수를 넣고 계속 진행하도록 훈련 받았으므로 이것이 처음에는 반 직관적 일 수 있습니다.
누군가 저에게 연락하여 질문에 대한 의견에 대한 답변을 자세하게 설명해달라고 요청했습니다. 그래서 여기 다른 사람을 도울 수 있도록 그 사람에게 내가 대답 한 내용이 있습니다.
모듈로 연산은 나머지 유클리드 분비물 (실수가 아닌 정수로만 작동)을 제공합니다. A = B * C + D (D <B)와 같은 A가있는 경우 A를 B로 유클리드 나누기의 몫은 C이고 나머지는 D입니다. 2를 4로 나누면 몫은 0이고 나머지는 2입니다.
자르지 못하는 A 개체가 있다고 가정합니다. 그리고 같은 양의 객체를 B 사람들에게 배포하려고합니다. B 개 이상의 객체가있는 한 각 객체에 1을 부여하고 반복합니다. B 개 미만의 개체가 남아 있으면 나머지 개체를 중지하고 유지합니다. 작업을 반복 한 횟수는 숫자 C라고 부르겠습니다. 마지막에 유지하는 객체의 수는 D라고합시다.
2 개의 물건과 4 명의 사람이 있다면. 이미 4 개 미만의 개체가 있습니다. 따라서 각 사람은 0 개의 객체를 얻습니다.
이것이 2 모듈로 4가 2 인 이유입니다.
모듈로 연산자는 두 정수 피연산자의 나눗셈을 평가합니다. 다음은 몇 가지 예입니다.
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)
mod는 나눈 알림을 의미합니다. 따라서 2를 4로 나눈 값은 0이며 2는 남아 있습니다. 따라서 2 mod 4는 2입니다.
모듈로는 수학 나누기 표현식의 나머지로, 정수로 표현됩니다.
따라서 화면의 너비가 100 픽셀이고 화면에 너비가 100 픽셀 인 위치 90에 화면에 픽셀이 있다고 가정하면 위치 10으로 줄 바꿈됩니다. 이유는 90 + 20 = 110이므로 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
When you divide 2 by 4, you get 0 with 2 left over or remaining. Modulo is just the remainder after dividing the number.
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.
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.
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
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 :)
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
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.
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).
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")
참고URL : https://stackoverflow.com/questions/1351925/why-does-2-mod-4-2
'Programming' 카테고리의 다른 글
쿼리 실행시 Sequelize가 콘솔에 SQL을 출력하지 못하게 하시겠습니까? (0) | 2020.06.13 |
---|---|
긴 단어가 내 div를 깨는 것을 방지하는 방법은 무엇입니까? (0) | 2020.06.13 |
JSON에서 문자열을 어떻게 이스케이프해야합니까? (0) | 2020.06.13 |
timedelta를 총 초로 변환 (0) | 2020.06.13 |
log4j vs 로그 백 (0) | 2020.06.13 |