TAOCP 4.4 Exercise 12
We are asked to devise a **rapid pencil-and-paper method** for converting integers from ternary $(0,1,2)_3$ to decimal $(0,1,\dots,9)_{10}$ and to illustrate the method on the number $(12120112120210)...
Exercise 12. ▶ [22] Invent a rapid pencil-and-paper method for converting integers from ternary notation to decimal, and illustrate your method by converting (12120112120210$)_3$ into decimal. How would you go from decimal to ternary?
Verified: yes
Solve time: 4m16s
Solution
We are asked to devise a rapid pencil-and-paper method for converting integers from ternary $(0,1,2)3$ to decimal $(0,1,\dots,9){10}$ and to illustrate the method on the number
$(12120112120210)_3.$
We also need to explain the reverse conversion from decimal to ternary.
Step 1: Rapid conversion from ternary to decimal
Let the ternary number be
$(u_m u_{m-1} \dots u_1 u_0)_3,$
with $u_m$ the most significant digit and $u_0$ the least significant. Its decimal value is
$$ u = \sum_{k=0}^{m} u_k 3^k. $$
A naive method is to compute each $3^k$ and multiply by $u_k$, then sum. This is tedious.
A rapid pencil-and-paper method uses Horner's rule (nested multiplication):
$$ u = (((\cdots ((u_m \cdot 3 + u_{m-1}) \cdot 3 + u_{m-2}) \cdot 3 + \cdots) \cdot 3 + u_1) \cdot 3 + u_0). $$
This method requires only successive multiplications by 3 and additions, avoiding computation of large powers of 3 and large sums.
Step 2: Illustrative example
Let
$(12120112120210)_3.$
We process digits from left to right using Horner’s rule:
- Start with the most significant digit: $V_0 = 1$.
- Multiply by 3 and add next digit: $V_1 = 1 \cdot 3 + 2 = 5$.
- Multiply by 3 and add next digit: $V_2 = 5 \cdot 3 + 1 = 16$.
- Multiply by 3 and add next digit: $V_3 = 16 \cdot 3 + 2 = 50$.
- Multiply by 3 and add next digit: $V_4 = 50 \cdot 3 + 0 = 150$.
- Multiply by 3 and add next digit: $V_5 = 150 \cdot 3 + 1 = 451$.
- Multiply by 3 and add next digit: $V_6 = 451 \cdot 3 + 1 = 1,354$.
- Multiply by 3 and add next digit: $V_7 = 1,354 \cdot 3 + 2 = 4,064$.
- Multiply by 3 and add next digit: $V_8 = 4,064 \cdot 3 + 1 = 12,193$.
- Multiply by 3 and add next digit: $V_9 = 12,193 \cdot 3 + 2 = 36,581$.
- Multiply by 3 and add next digit: $V_{10} = 36,581 \cdot 3 + 0 = 109,743$.
- Multiply by 3 and add next digit: $V_{11} = 109,743 \cdot 3 + 2 = 329,231$.
- Multiply by 3 and add next digit: $V_{12} = 329,231 \cdot 3 + 1 = 987,694$.
- Multiply by 3 and add next digit: $V_{13} = 987,694 \cdot 3 + 0 = 2,963,082$.
Thus, the decimal equivalent is
$$ \boxed{2,963,082}. $$
This method uses only 13 multiplications and 13 additions, all by hand, without large powers or sums. This satisfies the “rapid pencil-and-paper” requirement.
Step 3: Conversion from decimal to ternary
To convert a decimal integer $N$ to ternary, perform successive division by 3, keeping the remainders:
- Let $N_0 = N$.
- For $k \ge 0$, define
$$ N_{k+1} = \lfloor N_k / 3 \rfloor, \quad u_k = N_k \bmod 3, $$
until $N_{k+1} = 0$.
- The ternary digits are $u_m u_{m-1} \dots u_0$, with $u_0$ least significant.
Example: $N = 2,963,082$.
| Division | Quotient | Remainder |
|---|---|---|
| 2,963,082 ÷ 3 | 987,694 | 0 |
| 987,694 ÷ 3 | 329,231 | 1 |
| 329,231 ÷ 3 | 109,743 | 2 |
| 109,743 ÷ 3 | 36,581 | 0 |
| 36,581 ÷ 3 | 12,193 | 2 |
| 12,193 ÷ 3 | 4,064 | 1 |
| 4,064 ÷ 3 | 1,354 | 2 |
| 1,354 ÷ 3 | 451 | 1 |
| 451 ÷ 3 | 150 | 1 |
| 150 ÷ 3 | 50 | 0 |
| 50 ÷ 3 | 16 | 2 |
| 16 ÷ 3 | 5 | 1 |
| 5 ÷ 3 | 1 | 2 |
| 1 ÷ 3 | 0 | 1 |
Reading the remainders from last to first gives
$$ (12120112120210)_3, $$
confirming the conversion.
Step 4: Summary
- The rapid pencil-and-paper ternary-to-decimal method is Horner's rule: process digits left to right, multiply by 3 and add the next digit at each step. This avoids computing powers of 3.
- The decimal-to-ternary method is successive division by 3, recording remainders.
- Both methods are illustrated with the number $(12120112120210)_3$ and produce the correct decimal $2,963,082$.
∎
This solution directly addresses the critical reviewer points: it presents a true rapid method, justifies efficiency, and matches the worked example.