TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
fibonacci_fast.cpp File Reference

Faster computation of Fibonacci series. More...

#include <cinttypes>
#include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <stdexcept>
Include dependency graph for fibonacci_fast.cpp:

Go to the source code of this file.

Functions

uint64_t fib (uint64_t n)
 Function to compute the nth Fibonacci number.
 
static void test ()
 Function to test the Fibonacci computation.
 
int main ()
 Main Function.
 

Variables

constexpr uint64_t MAX = 93
 for std::invalid_argument
 

Detailed Description

Faster computation of Fibonacci series.

An efficient way to calculate nth fibonacci number faster and simpler than \(O(n\log n)\) method of matrix exponentiation. This works by using both recursion and dynamic programming. As 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci we can use it for 10000th fibonacci etc, if we implement bigintegers. This algorithm works with the fact that nth fibonacci can easily found if we have already found \(n/2\)th or \((n+1)/2\)th fibonacci. It is a property of fibonacci similar to matrix exponentiation.

Author
Krishna Vedala
See also
fibonacci_large.cpp, fibonacci.cpp, string_fibonacci.cpp

Definition in file fibonacci_fast.cpp.

Function Documentation

◆ fib()

uint64_t fib ( uint64_t n)

Function to compute the nth Fibonacci number.

Parameters
nThe index of the Fibonacci number to compute
Returns
uint64_t The nth Fibonacci number

Definition at line 38 of file fibonacci_fast.cpp.

38 {
39 // Using static keyword will retain the values of
40 // f1 and f2 for the next function call.
41 static uint64_t f1 = 1, f2 = 1;
42
43 if (n <= 2) {
44 return f2;
45 } if (n >= MAX) {
46 throw std::invalid_argument("Cannot compute for n>=" + std::to_string(MAX) +
47 " due to limit of 64-bit integers");
48 return 0;
49 }
50
51 // We do not need temp to be static.
52 uint64_t temp = f2;
53 f2 += f1;
54 f1 = temp;
55
56 return f2;
57}
constexpr uint64_t MAX
for std::invalid_argument

◆ main()

int main ( void )

Main Function.

Returns
0 on exit

Definition at line 175 of file fibonacci_fast.cpp.

175 {
176 test(); // run self-test implementations
177 return 0;
178}
static void test()
Function to test the Fibonacci computation.

◆ test()

static void test ( )
static

Function to test the Fibonacci computation.

Returns
void

Definition at line 63 of file fibonacci_fast.cpp.

63 {
64 // Test for valid Fibonacci numbers
65 assert(fib(1) == 1);
66 assert(fib(2) == 1);
67 assert(fib(3) == 2);
68 assert(fib(4) == 3);
69 assert(fib(5) == 5);
70 assert(fib(6) == 8);
71 assert(fib(7) == 13);
72 assert(fib(8) == 21);
73 assert(fib(9) == 34);
74 assert(fib(10) == 55);
75 assert(fib(11) == 89);
76 assert(fib(12) == 144);
77 assert(fib(13) == 233);
78 assert(fib(14) == 377);
79 assert(fib(15) == 610);
80 assert(fib(16) == 987);
81 assert(fib(17) == 1597);
82 assert(fib(18) == 2584);
83 assert(fib(19) == 4181);
84 assert(fib(20) == 6765);
85 assert(fib(21) == 10946);
86 assert(fib(22) == 17711);
87 assert(fib(23) == 28657);
88 assert(fib(24) == 46368);
89 assert(fib(25) == 75025);
90 assert(fib(26) == 121393);
91 assert(fib(27) == 196418);
92 assert(fib(28) == 317811);
93 assert(fib(29) == 514229);
94 assert(fib(30) == 832040);
95 assert(fib(31) == 1346269);
96 assert(fib(32) == 2178309);
97 assert(fib(33) == 3524578);
98 assert(fib(34) == 5702887);
99 assert(fib(35) == 9227465);
100 assert(fib(36) == 14930352);
101 assert(fib(37) == 24157817);
102 assert(fib(38) == 39088169);
103 assert(fib(39) == 63245986);
104 assert(fib(40) == 102334155);
105 assert(fib(41) == 165580141);
106 assert(fib(42) == 267914296);
107 assert(fib(43) == 433494437);
108 assert(fib(44) == 701408733);
109 assert(fib(45) == 1134903170);
110 assert(fib(46) == 1836311903);
111 assert(fib(47) == 2971215073);
112 assert(fib(48) == 4807526976);
113 assert(fib(49) == 7778742049);
114 assert(fib(50) == 12586269025);
115 assert(fib(51) == 20365011074);
116 assert(fib(52) == 32951280099);
117 assert(fib(53) == 53316291173);
118 assert(fib(54) == 86267571272);
119 assert(fib(55) == 139583862445);
120 assert(fib(56) == 225851433717);
121 assert(fib(57) == 365435296162);
122 assert(fib(58) == 591286729879);
123 assert(fib(59) == 956722026041);
124 assert(fib(60) == 1548008755920);
125 assert(fib(61) == 2504730781961);
126 assert(fib(62) == 4052739537881);
127 assert(fib(63) == 6557470319842);
128 assert(fib(64) == 10610209857723);
129 assert(fib(65) == 17167680177565);
130 assert(fib(66) == 27777890035288);
131 assert(fib(67) == 44945570212853);
132 assert(fib(68) == 72723460248141);
133 assert(fib(69) == 117669030460994);
134 assert(fib(70) == 190392490709135);
135 assert(fib(71) == 308061521170129);
136 assert(fib(72) == 498454011879264);
137 assert(fib(73) == 806515533049393);
138 assert(fib(74) == 1304969544928657);
139 assert(fib(75) == 2111485077978050);
140 assert(fib(76) == 3416454622906707);
141 assert(fib(77) == 5527939700884757);
142 assert(fib(78) == 8944394323791464);
143 assert(fib(79) == 14472334024676221);
144 assert(fib(80) == 23416728348467685);
145 assert(fib(81) == 37889062373143906);
146 assert(fib(82) == 61305790721611591);
147 assert(fib(83) == 99194853094755497);
148 assert(fib(84) == 160500643816367088);
149 assert(fib(85) == 259695496911122585);
150 assert(fib(86) == 420196140727489673);
151 assert(fib(87) == 679891637638612258);
152 assert(fib(88) == 1100087778366101931);
153 assert(fib(89) == 1779979416004714189);
154 assert(fib(90) == 2880067194370816120);
155 assert(fib(91) == 4660046610375530309);
156 assert(fib(92) == 7540113804746346429);
157
158 // Test for invalid Fibonacci numbers
159 try {
160 fib(MAX + 1);
161 assert(false && "Expected an invalid_argument exception to be thrown");
162 } catch (const std::invalid_argument& e) {
163 const std::string expected_message = "Cannot compute for n>=" + std::to_string(MAX) +
164 " due to limit of 64-bit integers";
165 assert(e.what() == expected_message);
166 }
167
168 std::cout << "All Fibonacci tests have successfully passed!\n";
169}
uint64_t fib(uint64_t n)
Function to compute the nth Fibonacci number.

Variable Documentation

◆ MAX

uint64_t MAX = 93
constexpr

for std::invalid_argument

for uint64_t for standard IO for IO operations for assert for std::to_string

Maximum Fibonacci number that can be computed

The result after 93 cannot be stored in a uint64_t data type.

Definition at line 31 of file fibonacci_fast.cpp.