Fibonacci高于100000

时间:2015-12-02 20:22:56

标签: java multithreading biginteger fibonacci

我搜索了很多斐波那契计划的例子。所有这些都不算大数。我的程序计算斐波那契(10000)。我的任务是Fibonacci(100000)和Fibonacci(200000)。你知道吗,也许它可以是线程?

City myCity;

int main(){
        if (myCity.getBeing(i,j) == null)
        {
            cout << "O";
        }
}

感谢所有答案。

1 个答案:

答案 0 :(得分:3)

如此多的递归调用,调用堆栈将溢出(更不用说它会非常慢)。请改用迭代算法,例如:

private BigInteger fib(int n) {
    BigInteger prev = BigInteger.ONE;
    BigInteger prevprev = BigInteger.ONE;
    BigInteger num = BigInteger.ONE;
    for (int i = 2; i < n; ++i) {
        num = prev.add(prevprev);
        prevprev = prev;
        prev = num;
    }
    return num;
}