Modular arithmetic
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Modular arithmetic is counting that wraps around, like the hours on a clock. Walk a hand around a dial of 12 spots, see why 14 mod 12 is the remainder 2, and then watch a byte do the same thing with 256 spots: 250 + 10 leaves 4, because the ninth bit has no room.
Clock arithmetic
This dial counts 0 to 11, then starts again. Start at 9 and add 5 hours: 10, 11, 0, 1, 2. You land on 2, not 14. Counting that wraps around like this is called modular arithmetic.
>>> 9 + 5 14 >>> (9 + 5) % 12 2
اقرأ الدرس خطوة بخطوة
Clock arithmetic
This dial counts 0 to 11, then starts again. Start at 9 and add 5 hours: 10, 11, 0, 1, 2. You land on 2, not 14. Counting that wraps around like this is called modular arithmetic.
>>> 9 + 5 14 >>> (9 + 5) % 12 2Mod is the remainder
14 mod 12 means: divide 14 by 12 and keep the remainder. 14 ÷ 12 = 1, remainder 2, so 14 mod 12 = 2. The 12 is called the modulus. Python writes mod as %: 14 % 12 gives 2.
>>> 14 % 12 2 >>> 14 // 12 # how many whole 12s fit 1Laps and a leftover
Any whole number splits into full laps of the dial plus a leftover: 14 = 1 × 12 + 2. The leftover is always 0 to 11, never 12 or more. divmod(14, 12) gives both parts: (1, 2).
>>> divmod(14, 12) (1, 2) >>> 1 * 12 + 2 14Same spot, same remainder
2, 14 and 26 all land on the same spot, because each is 2 more than some full laps. We write 26 ≡ 2 (mod 12) and say 26 is congruent to 2, mod 12. They share the remainder 2.
>>> [n % 12 for n in (2, 14, 26)] [2, 2, 2] >>> (26 - 2) % 12 # 12 divides the gap 0Shrink big jumps first
Big jumps can shrink first. 29 hours is 2 full laps plus 5, because 29 % 12 = 5. So 9 + 29 lands where 9 + 5 does: on 2. In code, (9 + 29) % 12 equals (9 + 5) % 12.
>>> 29 % 12 5 >>> (9 + 29) % 12, (9 + 5) % 12 (2, 2)Going backwards
Subtracting walks the other way round. From 0, step back 3: 11, 10, 9. So -3 mod 12 is 9. Python agrees: -3 % 12 gives 9, because Python's % always answers 0 to 11 when the modulus is 12.
>>> -3 % 12 9 >>> divmod(-3, 12) (-1, 9) >>> import math >>> math.fmod(-3, 12) # the C-style answer -3.0A byte wraps at 256
A byte has 8 bits, so it holds 0 to 255, then wraps like a dial with 256 spots. 250 + 10 = 260 needs a ninth bit, worth 256. A byte has no room for it, so it keeps 260 % 256 = 4.
>>> 250 + 10 260 >>> bin(260) '0b100000100' >>> (250 + 10) % 256 4Keep the low 8 bits
Mod by a power of two is cheap. Because 256 = 2⁸, x % 256 is just the lowest 8 bits of x. The & operator keeps a bit only where both numbers have a 1, so 260 & 0xFF (eight 1s) gives 4.
>>> 260 & 0xFF 4 >>> 260 % 256 4 >>> 173 % 2, 173 & 1 # the lowest bit (1, 1)Wrapping a list index
Programs use % to cycle through slots forever. With 12 slots, i = (i + 1) % 12 counts 10, 11, 0, 1: it never runs past the last slot. Ring buffers (lists reused in a circle), clocks and calendars all work this way.
>>> i = 10 >>> for _ in range(3): ... i = (i + 1) % 12 ... print(i) ... 11 0 1
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What does divmod(17, 5) return in Python?
divmod gives the quotient and the remainder together. 17 ÷ 5 = 3 with remainder 2, because 3 × 5 = 15 and 17 − 15 = 2. So 17 % 5 is 2.
02On a dial with spots 0 to 11, the hand points at 10. Where does it point after 7 more steps?
10 + 7 = 17, and 17 % 12 = 5: the hand passes 11, wraps to 0 and goes on to 5.
03What does Python print for -1 % 12?
One step back from 0 is 11. Python's % takes the sign of the modulus, so the answer is always 0 to 11 here. (In C or JavaScript, -1 % 12 is -1.)
04What does 300 & 0xFF give in Python?
300 = 1 0010 1100 in binary. 0xFF is eight 1s, and & keeps a bit only where both have a 1, so only the low 8 bits 0010 1100 = 44 survive. That is 300 % 256 = 44, just what a byte would hold.
05Which pair is congruent mod 12 (same remainder when divided by 12)?
17 − 5 = 12, which 12 divides exactly, so both leave remainder 5. The other pairs differ by 10, 11 and 10.
المسرد
- Modular arithmetic
- Arithmetic where numbers wrap around after reaching a fixed value, like the hours on a clock.
- Modulus
- The number you wrap around at, the n in a mod n. A 12-spot dial uses modulus 12; a byte uses 256.
- Remainder
- What is left after taking out as many whole copies of the divisor as fit: 14 ÷ 12 leaves 2.
- % (modulo operator)
- Python's operator for the remainder: 14 % 12 is 2. With a positive modulus the answer is never negative.
- divmod()
- A Python built-in that returns the quotient and the remainder together: divmod(14, 12) is (1, 2).
- Congruent (≡)
- a ≡ b (mod n) means a and b leave the same remainder mod n; equivalently, n divides a − b.
- Overflow (wrap-around)
- When a result needs more bits than a fixed-size integer has, the extra high bits are lost and the value wraps.
- Mask
- A bit pattern used with & to keep some bits and clear the rest: x & 0xFF keeps the low 8 bits of x.
الملاحظات والمصادر
- A real clock writes 12 where this dial writes 0. In maths the remainders mod 12 are counted from 0, so the spots are 0 to 11.
- a ≡ b (mod n) means n divides a − b with nothing left over: 26 − 2 = 24 = 2 × 12.
- Languages disagree about negative numbers. Python's % takes the sign of the modulus, so -3 % 12 is 9; in C, Java and JavaScript, -3 % 12 is -3.
- Python's own integers never wrap: 250 + 10 is simply 260. Fixed-size integers do: an unsigned 8-bit integer in C (uint8_t) is stored modulo 256.
- For a power of two, x % 2**k equals x & (2**k − 1) for any x ≥ 0: % 2 keeps the last bit (odd or even), % 256 keeps the last byte.
- Lehman, Leighton & Meyer, Mathematics for Computer Science (MIT 6.042J), chapter 9 Number Theory
- MIT OpenCourseWare 6.042J Mathematics for Computer Science
- Rosen, Discrete Mathematics and Its Applications, 8th ed., section 4.1 Divisibility and Modular Arithmetic
- Python reference: Binary arithmetic operations (% and //)
- Python library: divmod()
- Python library: math.fmod()
- cppreference: C arithmetic operators (unsigned arithmetic is modulo 2^N)
- Harris & Harris, Digital Design and Computer Architecture, 2nd ed., section 1.4.5 Binary Addition (overflow)
اختبار يتاح بعد الخطوة الأخيرة.