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

前端 未结 10 1551
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:00

    It's simple, in Java the solution looks like this:

    public int f(int n) {
    
          int tmp;
          int a = 3;
          int b = 1;
    
          for (int i = 0; i < n; i++) {
              tmp = a;
              a = 3 * a - b;
              b = tmp;
          }
    
          return b;
    
    }
    

    All recursive solutions can be transformed into iterative solutions (the opposite is also true, see this post), albeit it's easier if the recursive solution it's in tail-recursive form.

    The above algorithm can be understood as a dynamic-programming solution to the original recursion, it's very efficient since it only needs to save the two previous values at each point in the iteration.

    0 讨论(0)
  • 2020-12-01 19:07

    Here is simply a function with minimum line of code and maximum flexibility.

    You can add any "initial values" and any other recursive "function" you want simply.

    def fib(n):
      fibs = [1, 3]       # <--  your initial values here 
      if n == 1:
        return fibs[0]
      if n == 2:
        return fibs[:1]
      for i in range(2, n):
        fibs.append(3*fibs[-1] - fibs[-2])  # <-- your function here
      return fibs
    

    And the result is:

    n=10
    print(fib(n))
    
    [1, 3, 8, 21, 55, 144, 377, 987, 2584, 6765]
    
    0 讨论(0)
  • 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 )
    
    0 讨论(0)
  • 2020-12-01 19:19

    As requested by @paxdiablo I'm making this an answer. It's a recurrence relation and can be solved non-recursively, similar to the fibonacci sequence mentioned in another answer. It turns out to be (Python notation).

    def f(n):
        return int((13**0.5-3)/(2*13**0.5)*((3-13**0.5)/2)**n + (0.5+3/(2*13**0.5))*((3+13**0.5)/2)**n)
    

    However, this forumula does most probably not work for large n, because of limited float precision. The given python version fails for n = 30:

    >>> print ([f(n) == 3 * f(n-1) + f(n-2) for n in range(2, 30)])
    [True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, False]
    >>> print([f(n) for n in range(30)])
    [1, 3, 10, 33, 109, 360, 1189, 3927, 12970, 42837, 141481, 467280, 1543321, 5097243, 16835050, 55602393, 183642229, 606529080, 2003229469, 6616217487, 21851881930, 72171863277, 238367471761, 787274278560, 2600190307441, 8587845200883, 28363725910090, 93679022931153, 309400794703549, 1021881407041801]
    

    Warning: I used a "+" instead of a "-", so the formula is wrong. See comments.

    0 讨论(0)
提交回复
热议问题