What is a non recursive solution for Fibonacci-like sequence in Java?

前端 未结 10 1564
Happy的楠姐
Happy的楠姐 2020-12-01 18:23

Given this pseudo code of a function

f(0) = 1; 
f(1) = 3; 
f(n) = 3 * f(n - 1) - f(n - 2); // for n >= 2.

Is there a non recursive way o

10条回答
  •  醉话见心
    2020-12-01 19:13

    If your question is about whether an equivalent non-recursive definition of the function can be found, you should search for properties of the Fibonacci sequence.

    Your sequence can be found by writing the Fibonacci (without the first 2 numbers) and removing every 2nd number: 1, 3, 8, 21, 55, 144, ...

    sqrt5 = sqrt(5)
    phi = ( 1 + sqrt5 ) / 2
    fibonacci(n) = round( power( phi, n ) / sqrt5 ) 
    f(n) = fibonacci( 2*n + 2 )
    

提交回复
热议问题