Fibonacci sequence for n > 46 Java

后端 未结 4 1944
旧时难觅i
旧时难觅i 2021-01-23 22:38

I have the following code which provides the correct values for n < 47.

public static int fib(int n) {
    int nthTerm = 0;
    if (n == 2)
        nthTerm =          


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 23:24

    Use long instead of using int, and remember to cast the value from Math.round() to long as well (by writing (long) Math.round(...) just as you casted to int) .

提交回复
热议问题