2013-03-16 83 views
0

我正在學習java,正在練習數組。我決定生成一個斐波那契數列作爲實驗,並且不禁想到可能有更簡單的方法來生成這個系列(使用數組和循環)。有沒有比這更好的顯示斐波那契數列的方法?

有什麼想法?

//Generate a Fibonacci series 
public class Array { 

    public static void main(String[] args) { 
     // An array to store the values 
     int[] intArray = new int[20]; 

     // starting values for the sequence 
     intArray[0] = 0; 
     intArray[1] = 1; 

     //display the first values 
     System.out.println("array["+(0)+"] = "+intArray[0]); 
     System.out.println("array["+(1)+"] = "+intArray[1]); 

     //generate the fibonnacci progression with a loop 
     for (int count=2;count<intArray.length;count++){ 
     intArray[count] = intArray[(count-1)]+intArray[(count-2)]; 
     System.out.println("array["+(count)+"] = "+intArray[count]); 
    } 
} 

回答

0

我會說你所做的是,如果你想所有的值存儲在數組中解決這個問題的最優雅,最有效的方式方法。然而,存儲值不是必需的。

從審美的角度來看,你的count變量和數字0和1的圓括號是沒有必要的,並且使代碼相當混亂。

1

你應該尋找一個遞歸的答案,其中有很多這個網站。例如。 fibonacci series - recursive summation

+1

我總是儘管斐波那契是,當不使用遞歸的解決方案很好的例子。因爲最終彙總的唯一數字在遞歸尾部爲1,因此運行時間與結果成正比。 – devconsole 2013-03-16 00:36:26

+0

動態編程解決方案絕對沒有錯。 – Makoto 2013-03-16 00:38:07

+0

簡單的迭代解決方案也沒有問題,但遞歸解決方案是優雅的,效率不高,但問題在於優雅。 – 2013-03-16 00:40:56

1

這是一個無數組解決方案 - 僅使用了4個int

public class Fibonacci 
{ 
    public static void main(String[] args) 
    { 
     int first = 0; 
     int second = 1; 
     int sum; 
     for (int i = 0; i < 20; i++) 
     { 
     sum = first + second; 
     System.out.println("iteration " + i + ": " + sum); 
     first = second; 
     second = sum; 
     } 
    } 
} 

輸出:

iteration 0: 1 
iteration 1: 2 
iteration 2: 3 
iteration 3: 5 
iteration 4: 8 
iteration 5: 13 
iteration 6: 21 
iteration 7: 34 
iteration 8: 55 
iteration 9: 89 
iteration 10: 144 
iteration 11: 233 
iteration 12: 377 
iteration 13: 610 
iteration 14: 987 
iteration 15: 1597 
iteration 16: 2584 
iteration 17: 4181 
iteration 18: 6765 
iteration 19: 10946 
0

這是最短的,我可以做了:

public static void main(String[] args) { 
    int a = 0, b = 1; 
    long length = 20; 
    System.out.println(a); 
    System.out.println(b); 
    while (--length >= 0) 
     System.out.println((a = (b = a + b) - a) * 0 + b); 
} 

給出:

0 1 1 2 3 5 8 13 ...

0

測試過像這樣的一個解決方案嗎?它使用Moivre-Binet公式。長型我得到精度誤差對於n> 71.

public static void main(String[] args) { 
    for (int i = 0; i < 20; i++) { 
     System.out.println(getFibonacci(i)); 
    } 
} 

private static int getFibonacci(int n) { 
    return (int) ((1D/Math.sqrt(5D)) * ((Math.pow(((1D + Math.sqrt(5D))/2D), n)) - Math.pow(((1D - Math.sqrt(5D))/2D), n))); 
} 

越高Ñ較慢或存儲器飢餓的幼稚或遞歸算法。以下遞歸示例適用於我,最多可使用n = 14832。可能正在等待我的當前JVM設置。

static final Map<Integer,BigInteger> FIBONACCI_RESULTS = new HashMap<>(); 


private static BigInteger getFibonacciRecursive(final int n) { 
    return ((n == 1) || (n == 2)) ? BigInteger.ONE : fetchResult(n); 
} 

private static BigInteger fetchResult(final int n) { 
    BigInteger result; 
    System.out.println("n := "+n); 
    if (FIBONACCI_RESULTS.containsKey(n)) { 
     result = FIBONACCI_RESULTS.get(n); 
    } else { 
     result = getFibonacciRecursive(n - 1).add(getFibonacciRecursive(n - 2)); 
     FIBONACCI_RESULTS.put(n, result); 
    } 
    return result; 
} 
0

最優雅,結構合理的程序來生成斐波那契數列,我可以做的是:

import java.util.Scanner; 

public class fibon{ 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("How many times shall we generate the fibonacci series?"); 
     int max = scan.nextInt(); 
     scan.close(); 
     fibgen(max); 
    } 
    public static void fibgen(int max) { 
     int f = 0, s = 1; 
     for(int i = 0; i <= max; i++) { 
      f += s; 
      s = f - s; 
      System.out.println(s + " "); 
     } 

    } 
}