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.2-6

Exercise 1.2.2-6

not stated yet in Lean solution written

Rating10
KindK5, design an algorithm: give a method for a task, often with a cost bound from the written solution
SectionSection 1.2.2 of Volume 1. Companion chapter: Numbers, powers and logarithms

The task

Give a rule that compares two real numbers xx and yy from their decimal expansions.

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

Think of how words are put in dictionary order.

Hint 2

Use Eq. (2) to turn the first place where the expansions differ into a strict inequality between the numbers.

Solution

Show the solution

Let x=m+0.d1d2…x = m + 0.d_1d_2\ldots and y=n+0.e1e2…y = n + 0.e_1e_2\ldots. Compare the sequences (m,d1,d2,…)(m, d_1, d_2, \ldots) and (n,e1,e2,…)(n, e_1, e_2, \ldots) place by place. If they agree in every place, then x=yx = y. Otherwise let jj be the first place where they differ, and the number with the larger entry at place jj is the larger number.

Proof. If the sequences agree, then for each kk both xx and yy lie in the same interval of length 10−k10^{-k} by Eq. (2), so ∣x−y∣<10−k|x - y| < 10^{-k} for all kk and x=yx = y. Now suppose the first difference is at place j≥1j \ge 1 with dj<ejd_j < e_j. Let aa be the common value m+d1/10+⋯+dj−1/10j−1m + d_1/10 + \cdots + d_{j-1}/10^{j-1}. Eq. (2) gives x<a+(dj+1)/10j≤a+ej/10j≤yx < a + (d_j + 1)/10^j \le a + e_j/10^j \le y. If the first difference is in the integer part with m<nm < n, then Eq. (2) with k=1k = 1 gives x<m+1≤n≤yx < m + 1 \le n \le y. In both cases x<yx < y, so equal expansions are the only way to get x=yx = y. □\square

Check

Take x=0.1234…x = 0.1234\ldots and y=0.1239…y = 0.1239\ldots. The sequences (0,1,2,3,4,…)(0, 1, 2, 3, 4, \ldots) and (0,1,2,3,9,…)(0, 1, 2, 3, 9, \ldots) agree up to place 33, and the first difference is at place 44 with 4<94 < 9, so x<yx < y. The string 0.1999…0.1999\ldots is not legal. Its legal form is 0.2000…0.2000\ldots, and the rule then compares (0,2,0,…)(0, 2, 0, \ldots) with other expansions correctly.

Remarks

The rule needs that expansions never end in infinitely many nines. This is the point of the condition in Eq. (1).

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

In Lean

not stated yet The exercise is not stated in Lean yet.

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

Something is wrong? Report it.