这是我为查找第 n 个斐波那契数而编写的代码:
unsigned long long fib(int n)
{
unsigned long long u = 1, v = 1, t;
for(int i=2; i<=n; i++)
{
t = u + v;
u = v;
v = t;
}
return v;
}
虽然算法运行得非常快,但当 n>93 时,输出开始变得异常。我认为/知道这是因为 unsigned long long 的 64 位大小。我是 C++ 的新手,但有没有办法解决这个问题,这样我就能得到类似 fib(9999) 的答案?
谢谢
最佳答案
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.
The main target applications for GMP are cryptography applications and research, Internet security applications, algebra systems, computational algebra research, etc...
关于c++ - Unsigned Long Long 不会超过第 93 个斐波那契数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3125872/