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

Exercise 1.2.4-19

proved in Lean solution written

RatingM10
MarkersM (mathematical), recommended
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 1.2.4 of Volume 1. Companion chapter: Integer functions and elementary number theory

The task

Show that if n⊥mn \perp m, then some integer n′n' has nn′≡1(modm)nn' \equiv 1 \pmod m, using the extended Euclidean algorithm.

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 extended algorithm finds integers uu and vv with um+vn=gcd⁡(m,n)um + vn = \gcd(m, n).

Hint 2

Reduce this equation modulo mm.

Solution

Show the solution

Run Euclid's algorithm on (m,n)(m, n) and carry two coefficients along. Keep a list of triples (r,u,v)(r, u, v) with r=um+vnr = um + vn. Start with (m,1,0)(m, 1, 0) and (n,0,1)(n, 0, 1). When (r0,u0,v0)(r_0, u_0, v_0) and (r1,u1,v1)(r_1, u_1, v_1) are the last two triples, put q=⌊r0/r1⌋q = \lfloor r_0 / r_1 \rfloor and form (r0−qr1,u0−qu1,v0−qv1)(r_0 - qr_1, u_0 - qu_1, v_0 - qv_1). This new triple again has r=um+vnr = um + vn, because the relation is linear. The remainders decrease, so the process stops with r1=0r_1 = 0, and the last nonzero r0r_0 is gcd⁡(m,n)\gcd(m, n) (the usual proof of Euclid's algorithm). So at the end u0m+v0n=gcd⁡(m,n)=1u_0 m + v_0 n = \gcd(m, n) = 1.

Reduce modulo mm: v0n=1−u0m≡1(modm)v_0 n = 1 - u_0 m \equiv 1 \pmod m. So n′=v0n' = v_0 works. □\square

Example with m=40m = 40 and n=17n = 17. The triples are (40,1,0)(40, 1, 0), (17,0,1)(17, 0, 1), then (6,1,−2)(6, 1, -2) with q=2q = 2, then (5,−2,5)(5, -2, 5) with q=2q = 2, then (1,3,−7)(1, 3, -7) with q=1q = 1, then (0,−17,40)(0, -17, 40) with q=5q = 5. So 3⋅40−7⋅17=13 \cdot 40 - 7 \cdot 17 = 1 and n′=−7n' = -7, which is 3333 modulo 4040.

Check

17⋅33=561=14⋅40+117 \cdot 33 = 561 = 14 \cdot 40 + 1, so 17⋅33≡1(mod40)17 \cdot 33 \equiv 1 \pmod{40}. A computer run finds an inverse for each of the φ(40)=16\varphi(40) = 16 residues that are relatively prime to 4040.

Remarks

Exercise 1.2.4-28 gives a second formula, n′=nφ(m)−1 mod mn' = n^{\varphi(m) - 1} \bmod m, which is slower than Euclid's algorithm. The inverse is unique modulo mm, by Law B.

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.