TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Faster computation of Fibonacci series. More...
#include <cinttypes>
#include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <stdexcept>
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 | |
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.
Definition in file fibonacci_fast.cpp.
uint64_t fib | ( | uint64_t | n | ) |
Function to compute the nth Fibonacci number.
n | The index of the Fibonacci number to compute |
Definition at line 38 of file fibonacci_fast.cpp.
int main | ( | void | ) |
Main Function.
Definition at line 175 of file fibonacci_fast.cpp.
|
static |
Function to test the Fibonacci computation.
Definition at line 63 of file fibonacci_fast.cpp.
|
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.