Definition
Fibonacci sequence is a sequence that generats numbers from previous two numbers. In other world, Fibonacci sequence $a$ is a a sequence defined by follows.
$a_1 = a_2 = 1$, $a_n = a_{n - 1} + a_{n - 2} \forall n \ge 3$.
Fibonacci sequence can be changed to simple form as follows.
$a_n = a_{n-1} + a_{n-2}$
$\Leftrightarrow a_n + (\frac{\sqrt{5}-1}{2})a_{n-1} = (\frac{\sqrt{5} + 1}{2})(a_{n-1} + (\frac{\sqrt{5}-1}{2}) a_{n-2})$
$\Leftrightarrow a_{n+1} + (\frac{\sqrt{5}-1}{2})a_n = (\frac{\sqrt{5} + 1}{2})^{n - 1}(a_2 + (\frac{\sqrt{5}-1}{2})a_1) = (\frac{\sqrt{5} + 1}{2})^n$
$\Leftrightarrow a_{n+1} - \frac{1}{\sqrt{5}}(\frac{\sqrt{5} + 1}{2})^{n+1} = -(\frac{\sqrt{5}-1}{2})(a_n - \frac{1}{\sqrt{5}}(\frac{\sqrt{5} + 1}{2})^n)$
$\Leftrightarrow a_n - \frac{1}{\sqrt{5}}(\frac{\sqrt{5} + 1}{2})^n = (-(\frac{\sqrt{5}-1}{2}))^{n-1}(a_1 - \frac{1}{\sqrt{5}}(\frac{\sqrt{5} + 1}{2})) = -\frac{1}{\sqrt{5}}(\frac{1-\sqrt{5}}{2})^n$
As a result, $\Leftrightarrow a_n = \frac{1}{\sqrt{5}}(\frac{\sqrt{5} + 1}{2})^n - \frac{1}{\sqrt{5}}(\frac{1-\sqrt{5}}{2})^n$
or
$a_n = a_{n-1} + a_{n-2}$
$\Leftrightarrow a_n - (\frac{\sqrt{5}+1}{2})a_{n-1} = (\frac{1 - \sqrt{5} }{2})(a_{n-1} - (\frac{\sqrt{5}+1}{2}) a_{n-2})$
$\Leftrightarrow a_{n+1} - (\frac{\sqrt{5}+1}{2})a_n = (\frac{1 - \sqrt{5}}{2})^{n-1}(a_2 - (\frac{\sqrt{5}+1}{2})a_1) = (\frac{1 - \sqrt{5}}{2})^n$
$\Leftrightarrow a_{n+1} - (\frac{\sqrt{5}+1}{2})a_n = (\frac{1 - \sqrt{5}}{2})^n$
$\Leftrightarrow a_{n+1} + \frac{1}{\sqrt{5}}(\frac{1 - \sqrt{5}}{2})^{n+1}= (\frac{\sqrt{5}+1}{2})(a_n + \frac{1}{\sqrt{5}}(\frac{1 - \sqrt{5}}{2})^n)$
$\Leftrightarrow a_n + \frac{1}{\sqrt{5}}(\frac{1 - \sqrt{5}}{2})^n = (\frac{\sqrt{5}+1}{2})^{n-1}(a_1 + \frac{1}{\sqrt{5}}(\frac{1 - \sqrt{5}}{2})) = \frac{1}{\sqrt{5}}(\frac{\sqrt{5}+1}{2})^n$
$\Leftrightarrow a_n = \frac{1}{\sqrt{5}}(\frac{\sqrt{5}+1}{2})^n - \frac{1}{\sqrt{5}}(\frac{1 - \sqrt{5}}{2})^n$
As a result, $a_n = \frac{1}{\sqrt{5}}((\frac{\sqrt{5}+1}{2})^n - (\frac{1 - \sqrt{5}}{2})^n)$
Implementation
Recursion
It is the most naive and simplest implementation among all.
1 |
|
Dynamic programming
Top-down approach
1 |
|
Bottom-up approach
1 |
|
Mathematical analysis
From the formula $a_n = \frac{1}{\sqrt{5}}( ({\frac{1 + \sqrt{5}}{2}})^n - ({\frac{1 - \sqrt{5}}{2}})^n)$, Fibonacci sequence can be computed in logarithmic time with constant space. To apply this to the program, the following techniques have been used. First of all, it uses Multiply operation with the logarithmic algorithm. Secondly, it ignores one side of algorithm because it is Symmetry between and . This means it always has the same value for and . The proof is as follows.
$(a+b\sqrt{5})(c+d\sqrt{5}) = ac +5bd + (ad+bc)\sqrt{5}$
$(a-b\sqrt{5})(c-d\sqrt{5}) = ac +5bd - (ad+bc)\sqrt{5}$.
Therefore, this program computes only $\frac{1}{\sqrt{5}}({\frac{1 + \sqrt{5}}{2}})^n$. Notice that if the $\frac{1}{\sqrt{5}}(\frac{1 + \sqrt{5}}{2})^n$ is $\frac{1}{\sqrt{5}}(a + b\sqrt{5})$ then answer should be because answer is an integer and it has the same value from the other side.
Finally, program doesn’t compute $({\frac{1 + \sqrt{5}}{2}})^n$ but $\frac{(1 + \sqrt{5})^n}{2^n}$. This result in separate computation for $(1 + \sqrt{5})^n$ and ${2^n}$. Therefore, there couldn’t be a floating-point error. In the program below, it uses $a$ as an index of $1$, $b$ as an index of $\sqrt{5}$ and $div$ as an index of power of $2$. At the last step, it avoided division but used shift operation to reduce overhead. Notice that $a$ and $b$ will be divided in to two and $div$ will be subtracted by 1 if both $a$ and $b$ is even and $div$ is bigger than 1 to avoid overflow. $div$ will not go below 1 because it will be subtracted by one at the last step because the answer is $2b$ not $b$. Notice that it uses a logarithmic power operation to compute. In other world to compute $a^x$, it computes $a^1 + a^2 + a^4 + a^8 + \cdots$ depending on the binary digit of $x$.
1 |
|
Performance comparison
The mathematical approach is the fastest, the bottom-up approach is the second, top-down is the third, recursion is the last. Detailed test results are below.
Normal scale | Logarithmic scale | ||||||||
N | Mathematical | Bottom-up | Top-down | Recursion | Mathematical | Bottom-up | Top-down | Recursion | Local best |
1 | 10235 | 8792 | 6851 | 6630 | 9.23357 | 9.0816 | 8.83215 | 8.79936 | Recursion |
2 | 11630 | 8312 | 6708 | 6461 | 9.36134 | 9.02546 | 8.81106 | 8.77354 | Recursion |
3 | 14169 | 9876 | 49613 | 8054 | 9.55881 | 9.19786 | 10.812 | 8.99392 | Recursion |
4 | 13645 | 11052 | 50462 | 9608 | 9.52113 | 9.31037 | 10.829 | 9.17035 | Recursion |
5 | 16242 | 11927 | 68208 | 11720 | 9.69536 | 9.38656 | 11.1303 | 9.36905 | Recursion |
6 | 113054 | 19069 | 115477 | 24407 | 11.6356 | 9.85582 | 11.6568 | 10.1026 | Bottom-up |
7 | 27275 | 20290 | 147861 | 33960 | 10.2137 | 9.91788 | 11.904 | 10.4329 | Bottom-up |
8 | 24092 | 21890 | 158735 | 50019 | 10.0896 | 9.99379 | 11.975 | 10.8202 | Bottom-up |
9 | 27424 | 23129 | 221669 | 80795 | 10.2192 | 10.0488 | 12.3089 | 11.2997 | Bottom-up |
10 | 27408 | 24460 | 204569 | 125684 | 10.2186 | 10.1048 | 12.2287 | 11.7415 | Bottom-up |
11 | 30427 | 26093 | 229421 | 236295 | 10.3231 | 10.1694 | 12.3433 | 12.3728 | Bottom-up |
12 | 27203 | 27240 | 258010 | 333141 | 10.2111 | 10.2124 | 12.4608 | 12.7163 | Mathematical |
13 | 52887 | 28886 | 285534 | 656058 | 10.8759 | 10.2711 | 12.5621 | 13.394 | Bottom-up |
14 | 31111 | 31448 | 332717 | 921344 | 10.3453 | 10.3561 | 12.715 | 13.7336 | Mathematical |
15 | 34203 | 31631 | 343148 | 1411580 | 10.4401 | 10.3619 | 12.7459 | 14.1602 | Bottom-up |
16 | 27148 | 32943 | 351717 | 1568424 | 10.2091 | 10.4025 | 12.7706 | 14.2656 | Mathematical |
17 | 20980 | 23182 | 263037 | 2547217 | 9.95132 | 10.0511 | 12.48 | 14.7505 | Mathematical |
18 | 20869 | 24611 | 340437 | 4060424 | 9.94602 | 10.1109 | 12.738 | 15.2168 | Mathematical |
19 | 22926 | 25102 | 261359 | 6465351 | 10.04 | 10.1307 | 12.4737 | 15.682 | Mathematical |
20 | 20667 | 25864 | 280550 | 10501118 | 9.93629 | 10.1606 | 12.5445 | 16.167 | Mathematical |
21 | 22812 | 27024 | 293402 | 16985438 | 10.035 | 10.2045 | 12.5893 | 16.6479 | Mathematical |
22 | 23184 | 27835 | 301326 | 27372387 | 10.0512 | 10.234 | 12.6159 | 17.125 | Mathematical |
23 | 25186 | 28832 | 324435 | 44110219 | 10.134 | 10.2692 | 12.6898 | 17.6022 | Mathematical |
24 | 20888 | 29794 | 348050 | 71377722 | 9.94693 | 10.3021 | 12.7601 | 18.0835 | Mathematical |
25 | 22866 | 30591 | 354694 | 115484831 | 10.0374 | 10.3285 | 12.779 | 18.5646 | Mathematical |
26 | 23669 | 31610 | 359434 | 186855345 | 10.0719 | 10.3612 | 12.7923 | 19.0458 | Mathematical |
27 | 25091 | 32482 | 400048 | 302109566 | 10.1303 | 10.3884 | 12.8993 | 19.5263 | Mathematical |
28 | 22717 | 33741 | 386085 | 488550062 | 10.0309 | 10.4265 | 12.8638 | 20.007 | Mathematical |
29 | 24668 | 34483 | 412932 | 790141936 | 10.1133 | 10.4482 | 12.931 | 20.4877 | Mathematical |
30 | 25461 | 35500 | 423788 | 1279290871 | 10.1449 | 10.4773 | 12.957 | 20.9696 | Mathematical |
31 | 27316 | 38959 | 443156 | ... | 10.2152 | 10.5703 | 13.0017 | ... | Mathematical |
32 | 19889 | 37064 | 449541 | ... | 9.89792 | 10.5204 | 13.016 | ... | Mathematical |
33 | 22591 | 38108 | 487450 | ... | 10.0253 | 10.5482 | 13.0969 | ... | Mathematical |
34 | 22462 | 38795 | 478228 | ... | 10.0196 | 10.566 | 13.0778 | ... | Mathematical |
35 | 24950 | 41350 | 479806 | ... | 10.1246 | 10.6298 | 13.0811 | ... | Mathematical |
36 | 22503 | 42648 | 523592 | ... | 10.0214 | 10.6607 | 13.1685 | ... | Mathematical |
37 | 23147 | 43608 | 518030 | ... | 10.0496 | 10.683 | 13.1578 | ... | Mathematical |
38 | 24591 | 44482 | 555361 | ... | 10.1101 | 10.7028 | 13.2274 | ... | Mathematical |
39 | 27352 | 45739 | 638874 | ... | 10.2165 | 10.7307 | 13.3675 | ... | Mathematical |
40 | 22703 | 46887 | 647076 | ... | 10.0303 | 10.7555 | 13.3802 | ... | Mathematical |
41 | 24591 | 47835 | 624376 | ... | 10.1101 | 10.7755 | 13.3445 | ... | Mathematical |
42 | 24330 | 47897 | 596579 | ... | 10.0995 | 10.7768 | 13.299 | ... | Mathematical |
43 | 26275 | 48889 | 591725 | ... | 10.1764 | 10.7973 | 13.2908 | ... | Mathematical |
44 | 24497 | 49961 | 767949 | ... | 10.1063 | 10.819 | 13.5515 | ... | Mathematical |
45 | 26834 | 51646 | 648048 | ... | 10.1974 | 10.8522 | 13.3817 | ... | Mathematical |
46 | 26837 | 51758 | 739262 | ... | 10.1975 | 10.8543 | 13.5134 | ... | Mathematical |
47 | 29034 | 53703 | 679719 | ... | 10.2762 | 10.8912 | 13.4294 | ... | Mathematical |
48 | 22369 | 54462 | 698451 | ... | 10.0154 | 10.9053 | 13.4566 | ... | Mathematical |
49 | 24981 | 55278 | 719724 | ... | 10.1259 | 10.9201 | 13.4866 | ... | Mathematical |
50 | 24888 | 55849 | 704200 | ... | 10.1221 | 10.9304 | 13.4648 | ... | Mathematical |
51 | 26757 | 73331 | 797662 | ... | 10.1946 | 11.2027 | 13.5894 | ... | Mathematical |
52 | 24335 | 58261 | 738002 | ... | 10.0997 | 10.9727 | 13.5117 | ... | Mathematical |
53 | 26326 | 59784 | 785202 | ... | 10.1783 | 10.9985 | 13.5737 | ... | Mathematical |
54 | 26508 | 59313 | 856402 | ... | 10.1852 | 10.9906 | 13.6605 | ... | Mathematical |
55 | 28536 | 61089 | 791912 | ... | 10.2589 | 11.0201 | 13.5822 | ... | Mathematical |
56 | 24465 | 61904 | 788354 | ... | 10.105 | 11.0333 | 13.5777 | ... | Mathematical |
57 | 26499 | 100568 | 803439 | ... | 10.1849 | 11.5186 | 13.5967 | ... | Mathematical |
58 | 26163 | 63054 | 841987 | ... | 10.1721 | 11.0517 | 13.6435 | ... | Mathematical |
59 | 28944 | 63964 | 920700 | ... | 10.2731 | 11.0661 | 13.7329 | ... | Mathematical |
60 | 27185 | 65499 | 957073 | ... | 10.2104 | 11.0898 | 13.7716 | ... | Mathematical |
61 | 28128 | 66336 | 866843 | ... | 10.2445 | 11.1025 | 13.6726 | ... | Mathematical |
62 | 29034 | 67246 | 904581 | ... | 10.2762 | 11.1161 | 13.7152 | ... | Mathematical |
63 | 31755 | 144641 | 918552 | ... | 10.3658 | 11.882 | 13.7306 | ... | Mathematical |
64 | 22648 | 77184 | 912849 | ... | 10.0278 | 11.2539 | 13.7243 | ... | Mathematical |
65 | 24447 | 69598 | 929568 | ... | 10.1043 | 11.1505 | 13.7425 | ... | Mathematical |
66 | 24536 | 79183 | 919052 | ... | 10.1079 | 11.2795 | 13.7311 | ... | Mathematical |
67 | 27028 | 71351 | 940495 | ... | 10.2046 | 11.1754 | 13.7542 | ... | Mathematical |
68 | 41882 | 72961 | 955236 | ... | 10.6426 | 11.1977 | 13.7697 | ... | Mathematical |
69 | 27450 | 74149 | 989970 | ... | 10.2201 | 11.2138 | 13.8054 | ... | Mathematical |
70 | 26940 | 74165 | 987940 | ... | 10.2014 | 11.214 | 13.8034 | ... | Mathematical |
71 | 29203 | 84665 | 1004979 | ... | 10.282 | 11.3465 | 13.8205 | ... | Mathematical |
72 | 25054 | 76011 | 1133138 | ... | 10.1288 | 11.2386 | 13.9405 | ... | Mathematical |
73 | 26823 | 86682 | 1028957 | ... | 10.197 | 11.37 | 13.8441 | ... | Mathematical |
74 | 26832 | 87921 | 1043417 | ... | 10.1974 | 11.3842 | 13.858 | ... | Mathematical |
75 | 28852 | 88849 | 1165512 | ... | 10.2699 | 11.3947 | 13.9687 | ... | Mathematical |
76 | 27107 | 89961 | 1071402 | ... | 10.2075 | 11.4071 | 13.8845 | ... | Mathematical |
77 | 28693 | 90888 | 1079024 | ... | 10.2644 | 11.4174 | 13.8916 | ... | Mathematical |
78 | 29072 | 91962 | 1123328 | ... | 10.2775 | 11.4291 | 13.9318 | ... | Mathematical |
79 | 30962 | 83407 | 1174810 | ... | 10.3405 | 11.3315 | 13.9766 | ... | Mathematical |
80 | 24316 | 84163 | 1141752 | ... | 10.0989 | 11.3405 | 13.9481 | ... | Mathematical |
81 | 26752 | 96053 | 1272064 | ... | 10.1944 | 11.4727 | 14.0562 | ... | Mathematical |
82 | 27068 | 96959 | 1178098 | ... | 10.2061 | 11.482 | 13.9794 | ... | Mathematical |
83 | 28685 | 98479 | 1262305 | ... | 10.2641 | 11.4976 | 14.0484 | ... | Mathematical |
84 | 26923 | 98463 | 1290894 | ... | 10.2007 | 11.4974 | 14.0708 | ... | Mathematical |
85 | 29018 | 89035 | 1187264 | ... | 10.2757 | 11.3968 | 13.9872 | ... | Mathematical |
86 | 28239 | 101275 | 1243807 | ... | 10.2485 | 11.5256 | 14.0337 | ... | Mathematical |
87 | 30981 | 101661 | 1282043 | ... | 10.3411 | 11.5294 | 14.064 | ... | Mathematical |
88 | 26910 | 103627 | 1273582 | ... | 10.2003 | 11.5486 | 14.0573 | ... | Mathematical |
89 | 28589 | 91962 | 1242803 | ... | 10.2608 | 11.4291 | 14.0329 | ... | Mathematical |
90 | 28759 | 104886 | 1352592 | ... | 10.2667 | 11.5606 | 14.1175 | ... | Mathematical |
91 | 30796 | 106685 | 1380392 | ... | 10.3351 | 11.5776 | 14.1379 | ... | Mathematical |
92 | 28222 | 107592 | 1310485 | ... | 10.2479 | 11.5861 | 14.0859 | ... | Mathematical |