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-8

Exercise 1.2.4-8

proved in Lean solution written

Rating00
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 four remainders: 100 mod 3100 \bmod 3, 100 mod 7100 \bmod 7, −100 mod 7-100 \bmod 7 and −100 mod 0-100 \bmod 0.

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

Use x mod y=x−y⌊x/y⌋x \bmod y = x - y \lfloor x / y \rfloor and the rule x mod 0=xx \bmod 0 = x.

Solution

Show the solution

100 mod 3=100−3⌊33.33…⌋=100−99=1100 \bmod 3 = 100 - 3 \lfloor 33.33\ldots \rfloor = 100 - 99 = 1.

100 mod 7=100−7⌊14.28…⌋=100−98=2100 \bmod 7 = 100 - 7 \lfloor 14.28\ldots \rfloor = 100 - 98 = 2.

−100 mod 7=−100−7⌊−14.28…⌋=−100−7(−15)=5-100 \bmod 7 = -100 - 7 \lfloor -14.28\ldots \rfloor = -100 - 7(-15) = 5. The floor of a negative quotient goes down, so the quotient is −15-15, not −14-14.

−100 mod 0=−100-100 \bmod 0 = -100, by the rule for a zero modulus.

Check

Both 100−1100 - 1 and 100−2100 - 2 are multiples of 33 and 77 in turn (99=3⋅3399 = 3 \cdot 33, 98=7⋅1498 = 7 \cdot 14). −100−5=−105=7⋅(−15)-100 - 5 = -105 = 7 \cdot (-15). Each of the three remainders lies in the range [0,y)[0, y).

Remarks

The remainder of the % operator in C and Java has the sign of the dividend, so −100 % 7=−2-100 \,\%\, 7 = -2 there. The remainder of the book differs, as the value 55 shows.

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.