Why is my recursive function so slow in R?

前端 未结 7 1716
栀梦
栀梦 2020-12-02 23:51

The following takes about 30 seconds to run whereas I would expect it to be nearly instant. Is there a problem with my code?

x <- fibonacci(35);

fibonac         


        
相关标签:
7条回答
  • 2020-12-03 00:23

    If you are truly looking to return Fibonacci numbers and aren't using this example to explore how recursion works then you can solve it non-recursively by using the following:

    fib = function(n) {round((1.61803398875^n+0.61803398875^n)/sqrt(5))}
    
    0 讨论(0)
提交回复
热议问题