document fibonacci fast & link to fibonacci numbers

This commit is contained in:
Krishna Vedala 2020-05-27 18:58:21 -04:00
parent 0a58f8f08f
commit a53ba4b856
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
3 changed files with 29 additions and 14 deletions

View File

@ -6,7 +6,7 @@
* integer as input. * integer as input.
* \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f] * \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f]
* *
* @see fibonacci_large.cpp * @see fibonacci_large.cpp, fibonacci_fast.cpp
*/ */
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>

View File

@ -1,25 +1,39 @@
// An efficient way to calculate nth fibonacci number faster and simpler than /**
// O(nlogn) method of matrix exponentiation This works by using both recursion * @file
// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be * @brief Faster computation of Fibonacci series
// 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. * An efficient way to calculate nth fibonacci number faster and simpler than
// This algorithm works with the fact that nth fibonacci can easily found if we * \f$O(n\log n)\f$ method of matrix exponentiation This works by using both
// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci * recursion and dynamic programming. as 93rd fibonacci exceeds 19 digits, which
// similar to matrix exponentiation. * 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/2th or (n+1)/2th fibonacci It is a property
* of fibonacci similar to matrix exponentiation.
*
* @see fibonacci_large.cpp, fibonacci.cpp
*/
#include <cinttypes> #include <cinttypes>
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
/** maximum number that can be computed - The result after 93 cannot be stored
* in a `uint64_t` data type. */
const uint64_t MAX = 93; const uint64_t MAX = 93;
/** Array of computed fibonacci numbers */
uint64_t f[MAX] = {0}; uint64_t f[MAX] = {0};
/** Algorithm */
uint64_t fib(uint64_t n) { uint64_t fib(uint64_t n) {
if (n == 0) return 0; if (n == 0)
if (n == 1 || n == 2) return (f[n] = 1); return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
if (f[n]) return f[n]; if (f[n])
return f[n];
uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
@ -28,10 +42,11 @@ uint64_t fib(uint64_t n) {
return f[n]; return f[n];
} }
/** Main function */
int main() { int main() {
// Main Function // Main Function
for (uint64_t i = 1; i < 93; i++) { for (uint64_t i = 1; i < 93; i++) {
std::cout << i << " th fibonacci number is " << fib(i) << "\n"; std::cout << i << " th fibonacci number is " << fib(i) << std::endl;
} }
return 0; return 0;
} }

View File

@ -7,7 +7,7 @@
* Took 0.608246 seconds to compute 50,000^th Fibonacci * Took 0.608246 seconds to compute 50,000^th Fibonacci
* number that contains 10450 digits! * number that contains 10450 digits!
* *
* @see fibonacci.cpp * @see fibonacci.cpp, fibonacci_fast.cpp
*/ */
#include <cinttypes> #include <cinttypes>