If you want to use a library that can handle big integers, the simplest thing is to get the boost
library and use the cpp_int
type in the multiprecision library.
Since the multiprecision library is header-file only, this would be one of the simplest ones to use right out-of-box with minimal setup.
Here is a simple, straightforward implementation that computes the 1 millionth Fibonacci number without using floating point (which could yield inexact results):
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
int main() {
using Int = boost::multiprecision::cpp_int;
Int n1 = 0, n2 = 1;
Int n3;
for (long i = 1; i <= 1000000; ++i)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
}
std::cout << n3;
}
This took around 40 seconds to complete on an Intel I7 laptop, using Visual Studio 2019 in release mode.
The resulting number is a 208988 digit number.