c++ - Unsigned Long Long 不会超过第 93 个斐波那契数?

标签 c++ algorithm fibonacci

这是我为查找第 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) 的答案?

谢谢

最佳答案

http://gmplib.org/

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/

相关文章:

c++ - 需要帮助在类析构函数中释放内存

c++ - 根据 C++ 标准,类型不完整的 `sizeof(T)` 是否是有效的替换失败?

algorithm - 为 RPG 游戏生成随机数

ruby - 斐波那契线

c++ - Qt中的单元测试高度依赖(网络依赖)功能

c++ - QML 无法分配给不存在的属性

c++ - 给定这两个值的函数会产生第三个值?

java - 查找数字的所有排列的算法

c - 使用函数计算前 20 个斐波那契数及其总和

java - 将python斐波那契代码转换为java?