斐波那契數列增長極快:
Integer.MAX_VALUE (21億)Long.MAX_VALUE (9.22×10¹⁸)long 只能算到第 92 項,BigInteger 理論無上限(受限於內存)。
import java.math.BigInteger;
public class FibonacciBigInteger {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
System.out.println("第 " + i + " 項: " + fibonacci(i));
}
}
/**
* 迭代法計算斐波那契數列 (O(n) 時間, O(1) 空間)
* @param n 項數 (從 1 開始)
* @return BigInteger 結果
*/
public static BigInteger fibonacci(long n) {
if (n <= 2) {
return BigInteger.ONE; // F(1)=1, F(2)=1
}
BigInteger prev = BigInteger.ONE; // F(n-2)
BigInteger curr = BigInteger.ONE; // F(n-1)
BigInteger next = BigInteger.ZERO;
for (long i = 3; i <= n; i++) {
next = prev.add(curr); // F(n) = F(n-2) + F(n-1)
prev = curr;
curr = next;
}
return curr;
}
}
| 技術點 | 說明 |
|---|---|
BigInteger.ONE |
預定義常量,避免重複創建 new BigInteger("1") |
add() |
不可變對象,返回新實例,原對象不變 |
| 迭代法 vs 遞歸 | 遞歸 O(2ⁿ) 指數級爆炸,迭代 O(n) 線性,必須用迭代 |
| 空間優化 | 只保留前兩項,O(1) 空間,不存儲整個數列 |
public static BigInteger fibonacciFastDoubling(long n) {
if (n <= 1) return BigInteger.valueOf(n);
// 快速雙倍法公式:
// F(2k) = F(k) * [2*F(k+1) - F(k)]
// F(2k+1) = F(k+1)² + F(k)²
return fibonacciPair(n).first;
}
private static Pair fibonacciPair(long n) {
if (n == 0) return new Pair<>(BigInteger.ZERO, BigInteger.ONE);
Pair p = fibonacciPair(n >> 1); // n/2
BigInteger a = p.first; // F(k)
BigInteger b = p.second; // F(k+1)
BigInteger c = a.multiply(b.shiftLeft(1).subtract(a)); // F(2k)
BigInteger d = a.multiply(a).add(b.multiply(b)); // F(2k+1)
if ((n & 1) == 0) {
return new Pair<>(c, d);
} else {
return new Pair<>(d, c.add(d));
}
}
性能對比:n=1,000,000 時,迭代法需百萬次循環,快速雙倍法僅需 ~20 次遞歸(log₂n)。
new BigInteger("1") 創建常量:使用 BigInteger.ONE、BigInteger.ZERO、BigInteger.TENnew BigInteger("FF", 16) 解析十六進制divide(divisor, scale, RoundingMode.HALF_UP) 必須指定精度與舍入模式MutableBigInteger (內部類) 或專用庫如 Apache Commons MathBigInteger 讓 Java 輕鬆突破基本類型限制,處理任意大小整數運算,是高精度計算領域的基石工具。