Algorithm Search⌘K

Proofs: Fast doubling method for Fibonacci numbers

These are the human proofs of Fast doubling method for Fibonacci numbers, at full length.

Fast doubling returns the pair of Fibonacci numbers F(n) and F(n + 1) full

This proof shows: correctness, termination.

Let F0=0F_0 = 0, F1=1F_1 = 1 and Fm=Fm−1+Fm−2F_m = F_{m-1} + F_{m-2} for m≥2m \ge 2. Extend the sequence one step back by F−1=1F_{-1} = 1, so that F1=F0+F−1F_1 = F_0 + F_{-1} also holds. Let Q=(1110)Q = \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}.

Lemma 1 (matrix form). For each k≥0k \ge 0, Qk=(Fk+1FkFkFk−1)Q^k = \begin{pmatrix} F_{k+1} & F_k \\ F_k & F_{k-1} \end{pmatrix}.

Proof. By induction on kk. For k=0k = 0, Q0=I=(F1F0F0F−1)Q^0 = I = \begin{pmatrix} F_1 & F_0 \\ F_0 & F_{-1} \end{pmatrix}. If the claim holds for kk, then

Qk+1=QkQ=(Fk+1+FkFk+1Fk+Fk−1Fk)=(Fk+2Fk+1Fk+1Fk)Q^{k+1} = Q^k Q = \begin{pmatrix} F_{k+1} + F_k & F_{k+1} \\ F_k + F_{k-1} & F_k \end{pmatrix} = \begin{pmatrix} F_{k+2} & F_{k+1} \\ F_{k+1} & F_k \end{pmatrix}

by the recurrence, valid for the indices k+2≥2k + 2 \ge 2 and k+1≥1k + 1 \ge 1. □\square

Lemma 2 (doubling). For each k≥0k \ge 0,

F2k=Fk(2Fk+1−Fk),F2k+1=Fk2+Fk+12.F_{2k} = F_k (2F_{k+1} - F_k), \qquad F_{2k+1} = F_k^2 + F_{k+1}^2 .

Proof. By Lemma 1, Q2k=(Qk)2Q^{2k} = (Q^k)^2. The top right entry of (Qk)2(Q^k)^2 is Fk+1Fk+FkFk−1=Fk(Fk+1+Fk−1)F_{k+1}F_k + F_k F_{k-1} = F_k (F_{k+1} + F_{k-1}), and the top left entry is Fk+12+Fk2F_{k+1}^2 + F_k^2. The top right and top left entries of Q2kQ^{2k} are F2kF_{2k} and F2k+1F_{2k+1}. Finally Fk−1=Fk+1−FkF_{k-1} = F_{k+1} - F_k by the recurrence (for k=0k = 0 by the definition of F−1F_{-1}), so Fk+1+Fk−1=2Fk+1−FkF_{k+1} + F_{k-1} = 2F_{k+1} - F_k. □\square

Invariant. Let tL−1tL−2⋯t0t_{L-1} t_{L-2} \cdots t_0 be the binary digits of nn read by line L2, most significant first. After jj passes, let kj=∑i=0j−1tL−1−i 2j−1−ik_j = \sum_{i=0}^{j-1} t_{L-1-i}\, 2^{j-1-i} be the number formed by the first jj digits, so k0=0k_0 = 0, kj+1=2kj+tL−1−jk_{j+1} = 2k_j + t_{L-1-j} and kL=nk_L = n. The invariant is

(a,b)=(Fkj,Fkj+1).(a, b) = (F_{k_j}, F_{k_j + 1}) .

Initialization. Line L1 sets (a,b)=(0,1)=(F0,F1)(a, b) = (0, 1) = (F_0, F_1), and k0=0k_0 = 0.

Maintenance. Assume (a,b)=(Fk,Fk+1)(a, b) = (F_k, F_{k+1}) with k=kjk = k_j, and let tt be the next digit. By Lemma 2, line L3 sets c=Fk(2Fk+1−Fk)=F2kc = F_k(2F_{k+1} - F_k) = F_{2k} and line L4 sets d=Fk2+Fk+12=F2k+1d = F_k^2 + F_{k+1}^2 = F_{2k+1}. If t=0t = 0, then kj+1=2kk_{j+1} = 2k, and line L5 sets (a,b)=(F2k,F2k+1)(a, b) = (F_{2k}, F_{2k+1}). If t=1t = 1, then kj+1=2k+1k_{j+1} = 2k + 1, and line L6 sets (a,b)=(F2k+1,F2k+F2k+1)=(F2k+1,F2k+2)(a, b) = (F_{2k+1}, F_{2k} + F_{2k+1}) = (F_{2k+1}, F_{2k+2}) by the recurrence. In both cases (a,b)=(Fkj+1,Fkj+1+1)(a, b) = (F_{k_{j+1}}, F_{k_{j+1}+1}).

Termination. The loop runs once for each of the LL binary digits of nn.

Theorem. FAST-DOUBLING(n)(n) returns (Fn,Fn+1)(F_n, F_{n+1}) for each n≥0n \ge 0.

Proof. After the LL passes, the invariant gives (a,b)=(FkL,FkL+1)=(Fn,Fn+1)(a, b) = (F_{k_L}, F_{k_L + 1}) = (F_n, F_{n+1}). For n=0n = 0 the single digit 0 gives c=0c = 0, d=1d = 1 and (a,b)=(0,1)(a, b) = (0, 1). ■\blacksquare

Modular version. Lemma 2 is an identity of integers, so it also holds modulo mm: if a≡Fka \equiv F_k and b≡Fk+1(modm)b \equiv F_{k+1} \pmod m, then c≡F2kc \equiv F_{2k} and d≡F2k+1(modm)d \equiv F_{2k+1} \pmod m, because sums, differences and products respect congruences. Reducing aa and bb after each pass keeps the invariant modulo mm, and the result is Fn mod mF_n \bmod m.

Back to Fast doubling method for Fibonacci numbers