taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.2.4-9

Exercise 1.2.4-9

proved in Lean solution written

Rating05
KindK3, find a value: compute a number, a set or a closed form from the written solution
SectionSection 1.2.4 of Volume 1. Companion chapter: Integer functions and elementary number theory

The task

Evaluate 5 mod (−3)5 \bmod (-3), 18 mod (−3)18 \bmod (-3) and −2 mod (−3)-2 \bmod (-3).

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

The modulus is negative. Use the same formula x−y⌊x/y⌋x - y\lfloor x/y \rfloor and watch the floor of a negative quotient.

Solution

Show the solution

5 mod (−3)=5−(−3)⌊5/(−3)⌋=5+3⌊−1.67⌋=5+3(−2)=−15 \bmod (-3) = 5 - (-3)\lfloor 5/(-3) \rfloor = 5 + 3 \lfloor -1.67 \rfloor = 5 + 3(-2) = -1.

18 mod (−3)=18+3⌊−6⌋=18−18=018 \bmod (-3) = 18 + 3 \lfloor -6 \rfloor = 18 - 18 = 0.

−2 mod (−3)=−2+3⌊2/3⌋=−2+0=−2-2 \bmod (-3) = -2 + 3 \lfloor 2/3 \rfloor = -2 + 0 = -2.

Check

For a negative modulus yy the result lies in (y,0](y, 0], here in (−3,0](-3, 0]. The three values −1-1, 00 and −2-2 do. Also 5−(−1)=6=(−3)(−2)5 - (-1) = 6 = (-3)(-2) and −2−(−2)=0-2 - (-2) = 0.

Remarks

Exercise 1.2.4-16 proves that the remainder is the only number in the range that differs from xx by a multiple of yy, for y>0y > 0. The same holds for y<0y < 0 with the range (y,0](y, 0].

This solution is checked, and a person has not reviewed it yet. The source.

In Lean

proved The statement and its proof are in Lean. The kernel accepts the proof, and it uses the standard axioms only.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.