Achieve Fibonacci columns algorithm - Recursive and non-recursive

Fibonacci columns:

        The Fibonacci a column refers to a number of columns 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765 , 10946,17711,28657,46368 ........

The number of columns from the beginning of paragraph 3, each of which is equal to the sum of the first two.

Code:

The following is a deed that number Fibonacci implemented with java code column recursive and non-recursive algorithm

Package com.bug1;
 public  class the Main {
     public  static  void main (String [] args) { 
        System.out.println ( "recursive Fibonacci number" ); 
        System.out.println (FIB ( . 4 )); 
        the System .out.println ( "non-recursive Fibonacci number" ); 
        System.out.println (FIB2 ( . 4 )); 
    } 
    / * 
     * = n-2. 3. 1. 4. 5. 6. 7 
     * SUM = 2. 3. 1. 5. 1 8 
     * 
     * * / 
    // recursive the Fibonacci columns 
    public  static  int FIB ( int n-) {
         IF(n-<= 2) return . 1 ;
         return FIB (. 1-n-) + FIB (2-n- );         
    } 
    // non-recursive the Fibonacci columns 
public  static  int FIB2 ( int n-) {
     IF (n-<= 2 ) return . 1 ;
     int First =. 1 ;
     int SECOND =. 1 ;
     int SUM = 0 ;
     the while (n-> 2 ) { 
    SUM = First + SECOND; 
    First = SECOND; 
    SECOND = SUM; 
    n- -  ;
    }    
    return second;
}    
}

operation result:

Guess you like

Origin www.cnblogs.com/it1997/p/12066127.html