使用大数字:
public static void main(String[] args) {
// Precalculate enough fibs
List<BigInteger> list = new ArrayList<BigInteger>();
list.add(BigInteger.ONE);
list.add(BigInteger.ONE);
for (int i = 2; i < 512; i++) {
list.add(list.get(i - 2).add(list.get(i - 1)));
}
// Print first 10
for (int n = 2; n <= 10; n++) {
System.out.printf("fibonacci(%d)\t→\t%s%n", n, list.subList(0, n));
}
}
输出:
fibonacci(2) → [1, 1]
fibonacci(3) → [1, 1, 2]
fibonacci(4) → [1, 1, 2, 3]
fibonacci(5) → [1, 1, 2, 3, 5]
fibonacci(6) → [1, 1, 2, 3, 5, 8]
fibonacci(7) → [1, 1, 2, 3, 5, 8, 13]
fibonacci(8) → [1, 1, 2, 3, 5, 8, 13, 21]
fibonacci(9) → [1, 1, 2, 3, 5, 8, 13, 21, 34]
fibonacci(10) → [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]