Java Program Fibonacci Sequence

前端 未结 15 1843
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 03:17

I am writing a \"simple\" program to determine the Nth number in the Fibonacci sequence. Ex: the 7th number in the sequence is: 13. I have finished writing the program, it

15条回答
  •  無奈伤痛
    2020-12-17 03:48

    Too slow...

    Better: (JavaScript example)

    function fibonacci(n) {
        var a = 0, b = 1;
    
        for (var i = 0; i < n; i++) {
            a += b;
            b = a - b;
        }
        return a;
    }
    

提交回复
热议问题