题目
以下是斐波那契数列的递归定义:
Fn=Fn−1+Fn−2,F1=1,F2=1 .
那么前12项为:F1=1
F2=1
F3=2
F4=3
F5=5
F6=8
F7=13
F8=21
F9=34
F10=55
F11=89
F12=144
因此第12项, F12 ,是第一个包含三位数字的项。斐波那契数列中第一个包含1000位数字的项是第几项?
解题方法
采用java BigInteger类解题,或者自定义一个利用数组分段存储的大数类。后者的实现可以点击这里。
程序
public static void solve() {
BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.ONE;
BigInteger c = null;
int i = 2;
do {
c = a.add(b);
a = b;
b = c;
i++;
} while (c.toString().length() < 1000);
System.out.println(i);
}