给定一个区间求斐波那契数的个数,利用循环递推即可
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
BigInteger a,b;
BigInteger ZERO = BigInteger.ZERO;
while(in.hasNext())
{
a = in.nextBigInteger();
b = in.nextBigInteger();
if(a.compareTo(ZERO)==0 && b.compareTo(ZERO)==0)
break;
System.out.println(Fib(a,b));
}
}
//第一次写函数啊
public static int Fib(BigInteger a,BigInteger b)
{
int sum = 0;
BigInteger f = new BigInteger("1");
BigInteger s = new BigInteger("2");
BigInteger tmp;
while(true)
{
if(f.compareTo(b)>0)
break;
if(f.compareTo(a) >= 0)
sum++;
tmp = f;
f = s;
s = s.add(tmp);
}
return sum;
}
}