Hi, I wrote the program to calculate the Fibonacci numbers.
one of exercises is that to calculate 100th Fibonacci number, using int.
but the problem occurs, because overflow happens.
Then I've decided to use ULLong int but even 64-bit int cant support that, because integer of that size have maximum 18 digits and fib 100 have around 19.
So I decided to solve the problem using the array with the size of 2 and the modulo size of 1e17.
The good news is that I get is that the numbers no longer overflows, but the bad news is that I get the wrong result, I tried numerous times to solve this issue but that didn't worked for me.
Also the array with elementsize of 2 should represent 128 bit integer.
this is my code so far:
When I fix your code to compile it gets the right answer for fib(100) but fails for any other number:
1 2 3 4 5
std::deque<unsignedlonglong> vec;
void Fibonacchi(int x)
{
vec.clear();
unsignedlonglong a = 0, b = 1, mod = 10000000000000000000ULL;
I think the problem is that you have to use two 64-bit values to represent a and b also. At that point, you might as well use a class. Here's a version similar to lastchance's, but with two unsigned long long like you had:
#include <iostream>
#include <cmath>
usingnamespace std;
struct BigUnsigned {
BigUnsigned(): msd(0), lsd(0) {} // most and least significant digits
BigUnsigned(unsigned val): msd(0), lsd(val) {}
unsignedlonglong msd, lsd;
BigUnsigned &operator+=(const BigUnsigned &rhs)
{
constunsignedlonglong mod = 10000000000000000000ULL;
lsd += rhs.lsd;
msd += rhs.msd;
while (lsd >= mod) {
++msd;
lsd -= mod;
}
return *this;
}
BigUnsigned operator+(BigUnsigned &rhs)
{
BigUnsigned result(*this);
result += rhs;
return result;
}
};
ostream & operator << (ostream &os, const BigUnsigned & val)
{
if (val.msd) os << val.msd;
return os << val.lsd;
}
template<class T>
T Fib(int n)
{
T v1{1}, v2{1};
T last;
for (int i=2; i<n; ++i) {
last = v1;
v1 = v1+v2;
v2 = last;
}
return v1;
}
int
main(int argc, char **argv)
{
int N = atoi(argv[1]);
cout << " As BigUnsigned: " << Fib<BigUnsigned>(N) << '\n';
cout << "As unsigned long long: " << Fib<unsignedlonglong>(N) << '\n';
cout << " As double: " << Fib<double>(N) << '\n';
}
dhayden@DHAYDENHTZK3M2 ~/tmp
$ ./foo 17
As BigUnsigned: 1597
As unsigned long long: 1597
As double: 1597
dhayden@DHAYDENHTZK3M2 ~/tmp
$ ./foo 50
As BigUnsigned: 12586269025
As unsigned long long: 12586269025
As double: 1.25863e+10
dhayden@DHAYDENHTZK3M2 ~/tmp
$ ./foo 93
As BigUnsigned: 12200160415121876738
As unsigned long long: 12200160415121876738
As double: 1.22002e+19
dhayden@DHAYDENHTZK3M2 ~/tmp
$ ./foo 94
As BigUnsigned: 19740274219868223167
As unsigned long long: 1293530146158671551 (unsigned long long overflows at 94)
As double: 1.97403e+19
dhayden@DHAYDENHTZK3M2 ~/tmp
$ ./foo 100
As BigUnsigned: 354224848179261915075
As unsigned long long: 3736710778780434371
As double: 3.54225e+20
dhayden@DHAYDENHTZK3M2 ~/tmp
$ ./foo 184
As BigUnsigned: 127127879743834334146972278486287885163
As unsigned long long: 10993266775918486379
As double: 1.27128e+38
dhayden@DHAYDENHTZK3M2 ~/tmp
$ ./foo 185
As BigUnsigned: 21229789606137712014223751303346572685 (BigUnsigned overflows at 185)
As unsigned long long: 3465294890923511181
As double: 2.05697e+38