mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
compute arbitrarily large fibonacci number
This commit is contained in:
parent
72ac15ed01
commit
1cb1641abc
51
others/fibonacci_large.cpp
Normal file
51
others/fibonacci_large.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* Computes N^th Fibonacci number given as
|
||||||
|
* input argument. Uses custom build arbitrary integers library
|
||||||
|
* to perform additions and other operations.
|
||||||
|
*
|
||||||
|
* Took 0.608246 seconds to compute 50,000^th Fibonacci
|
||||||
|
* number that contains 10450 digits!
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include "large_number.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
large_number fib(unsigned long long n)
|
||||||
|
{
|
||||||
|
large_number f0(1);
|
||||||
|
large_number f1(1);
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
large_number f2 = f0 + f1;
|
||||||
|
f0 = f1;
|
||||||
|
f1 = f2;
|
||||||
|
n--;
|
||||||
|
} while (n > 2); // since we start from 2
|
||||||
|
return f1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
unsigned long long N;
|
||||||
|
if (argc == 2)
|
||||||
|
N = strtoull(argv[1], NULL, 10);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Enter N: ";
|
||||||
|
std::cin >> N;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto start_time = std::chrono::high_resolution_clock::now();
|
||||||
|
large_number result = fib(N);
|
||||||
|
auto end_time = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> time_taken = end_time - start_time;
|
||||||
|
|
||||||
|
std::cout
|
||||||
|
<< std::endl
|
||||||
|
<< N << "^th Fibonacci number: " << result << std::endl
|
||||||
|
<< "Number of digits: " << result.num_digits() << std::endl
|
||||||
|
<< "Time taken: " << time_taken.count() << " s" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user