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

前端 未结 10 1568
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 18:53

    The Fibonacci series sequence of numbers starts as: 0,1,1,2,3,5,8,13,21,34,55....

    this can be defined by simple recurrence relation F(n)=F(n-1)+F(n-2) for n>1 and two initial conditions, F(0)=1 and F(1)=1

    Algorithm Fibonacci

    //Computes the nth Fibonacci number

    //Input: A non-negative integer

    //Output: The nth Fibonacci number

    1. Begin Fibo
    2. integer n, i;
    3.    if n<=1 then
    4.     return n;
    5.  else
    6.    F(0)<-0; F(1)<-1;
    7.    for i<-2 to n do
    8.     F(i)<-F(i-1)+F(i-2);
    9.     F(i-2)=F(i-2);
    10.    F(i-1)=F(i);
    11. done
    12. end if
    13. end Fibo
    

提交回复
热议问题