Algorithm Search⌘K

Proofs: Double dabble

These are the human proofs of Double dabble, at full length.

Double dabble produces the decimal digits of its input full

This proof shows: correctness, termination.

Let x=∑k=0n−1xk2kx = \sum_{k=0}^{n-1} x_k 2^k with xk∈{0,1}x_k \in \{0, 1\}, and d≥1d \ge 1 with 2n−1<10d2^n - 1 < 10^d; the choice dd = the number of decimal digits of 2n−12^n - 1 satisfies this. For 0≤k≤n0 \le k \le n let

Xk=⌊x2n−k⌋=∑m=0k−1xn−1−m 2k−1−m,X_k = \left\lfloor \frac{x}{2^{n-k}} \right\rfloor = \sum_{m=0}^{k-1} x_{n-1-m} \, 2^{k-1-m},

the number formed by the first kk bits. Then X0=0X_0 = 0, Xk+1=2Xk+xn−1−kX_{k+1} = 2X_k + x_{n-1-k} and Xn=xX_n = x.

Invariant. After kk passes of the loop of line L2: (I1) 0≤D[i]≤90 \le D[i] \le 9 for each ii, and (I2) ∑i=0d−1D[i] 10i=Xk\sum_{i=0}^{d-1} D[i]\, 10^i = X_k.

Initialization. Line L1 sets all digits to 0, and X0=0X_0 = 0.

Maintenance. Assume (I1) and (I2) after kk passes, and let DD be the digits at that moment and b=xn−1−kb = x_{n-1-k} the next bit. Lines L3 and L4 set E[i]=D[i]+3E[i] = D[i] + 3 if D[i]≥5D[i] \ge 5 and E[i]=D[i]E[i] = D[i] otherwise; by (I1), 0≤E[i]≤120 \le E[i] \le 12. Lines L5 to L8 compute, for i=0,…,d−1i = 0, \ldots, d - 1, with c0=bc_0 = b,

ti=2E[i]+ci,D′[i]=ti mod 16,ci+1=⌊ti/16⌋.t_i = 2E[i] + c_i, \qquad D'[i] = t_i \bmod 16, \qquad c_{i+1} = \lfloor t_i / 16 \rfloor .

We show by cases that ci∈{0,1}c_i \in \{0, 1\} for each ii and that

D′[i]+10 ci+1=2D[i]+ci,0≤D′[i]≤9.(∗)D'[i] + 10\, c_{i+1} = 2D[i] + c_i, \qquad 0 \le D'[i] \le 9 . \tag{$*$}

Assume ci∈{0,1}c_i \in \{0, 1\}, which holds for i=0i = 0.

  • If D[i]≤4D[i] \le 4, then E[i]=D[i]E[i] = D[i] and ti=2D[i]+ci≤9<16t_i = 2D[i] + c_i \le 9 < 16, so D′[i]=tiD'[i] = t_i and ci+1=0c_{i+1} = 0, and (∗)(*) holds.
  • If D[i]≥5D[i] \ge 5, then E[i]=D[i]+3E[i] = D[i] + 3 and ti=2D[i]+ci+6t_i = 2D[i] + c_i + 6. From 5≤D[i]≤95 \le D[i] \le 9 we get 16≤ti≤25<3216 \le t_i \le 25 < 32, so ci+1=1c_{i+1} = 1 and D′[i]=ti−16=2D[i]+ci−10D'[i] = t_i - 16 = 2D[i] + c_i - 10, which lies in {0,…,9}\{0, \ldots, 9\} because 10≤2D[i]+ci≤1910 \le 2D[i] + c_i \le 19. Then D′[i]+10ci+1=2D[i]+ciD'[i] + 10 c_{i+1} = 2D[i] + c_i, and (∗)(*) holds.

In both cases ci+1∈{0,1}c_{i+1} \in \{0, 1\}, so the induction on ii goes through. Multiply (∗)(*) by 10i10^i and sum over i=0,…,d−1i = 0, \ldots, d-1:

∑i=0d−1D′[i] 10i+∑i=0d−1ci+110i+1=2∑i=0d−1D[i] 10i+∑i=0d−1ci10i.\sum_{i=0}^{d-1} D'[i]\, 10^i + \sum_{i=0}^{d-1} c_{i+1} 10^{i+1} = 2\sum_{i=0}^{d-1} D[i]\, 10^i + \sum_{i=0}^{d-1} c_i 10^i .

The carry terms with 1≤i≤d−11 \le i \le d - 1 cancel, which leaves

∑i=0d−1D′[i] 10i+cd10d=2Xk+b=Xk+1.\sum_{i=0}^{d-1} D'[i]\, 10^i + c_d 10^d = 2X_k + b = X_{k+1} .

Since Xk+1≤x≤2n−1<10dX_{k+1} \le x \le 2^n - 1 < 10^d and the sum on the left is nonnegative, cd=0c_d = 0. So ∑iD′[i] 10i=Xk+1\sum_i D'[i]\, 10^i = X_{k+1}, and (I1) holds for D′D' by (∗)(*).

Termination. The loop of line L2 runs once for each of the nn bits, and the inner loops run dd times each, so the algorithm stops after nn passes.

Theorem. DOUBLE-DABBLE returns digits D[0],…,D[d−1]∈{0,…,9}D[0], \ldots, D[d-1] \in \{0, \ldots, 9\} with ∑iD[i] 10i=x\sum_i D[i]\, 10^i = x, which are the decimal digits of xx, padded with zeros to dd places.

Proof. After nn passes, (I1) and (I2) give ∑iD[i] 10i=Xn=x\sum_i D[i]\, 10^i = X_n = x with digits in {0,…,9}\{0, \ldots, 9\}. The decimal representation with dd digits is unique, so these are the decimal digits of xx. ■\blacksquare

Back to Double dabble