3 * Compute elements of the Fibonacci sequence
5 * (c) 2024 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
32 /*----- Main code ---------------------------------------------------------*/
34 unsigned long recfib(unsigned n)
35 { return (n <= 1 ? n : recfib(n - 1) + recfib(n - 2)); }
37 unsigned long iterfib(unsigned n)
39 unsigned long u, v, t;
41 for (u = 0, v = 1; n--; t = v, v = u, u += t);
45 unsigned long expfib(unsigned n)
47 unsigned long a, b, u, v, t;
49 /* We work in %$\Q(\phi)$%, where %$\phi^2 = \phi + 1$%. I claim that
50 * %$\phi^k = F_k \phi + F_{k-1} \pmod f(\phi))$%. Proof by induction:
51 * note that * %$F_{-1} = F_1 - F_0 = 1$%, so %$\phi^0 = 1 = {}$%
52 * %$F_0 \phi + F_{-1}$%; and %$\phi^{k+1} = F_k \phi^2 + {}$%
53 * %$F_{k-1} \phi = F_k (\phi + 1) + F_{k-1} \phi = (F_k + {}$%
54 * %$F_{k-1} \phi + F_k = F_{k+1} \phi + F_k$% as claimed.
56 * Now, notice that %$(a \phi + b) (c \phi + d) = a c \phi^2 + {}$%
57 * $%(a d + b c) \phi + b d = a c (\phi + 1) + (a d + b c) \phi + {}$%
58 * %$b d = (a c + a d + b c) \phi + (a c + b d)$%. In particular,
59 * %$(u \phi + v)^2 \equiv (u^2 + 2 u v) \phi + (u^2 + v^2)$%.
61 a = 0, b = 1; u = 1, v = 0;
64 if (n%2) { t = a*u; a = t + a*v + b*u; b = t + b*v; }
65 n /= 2; if (!n) break;
66 t = u*u; u = t + 2*u*v; v = t + v*v;
71 /*----- That's all, folks -------------------------------------------------*/