How Many Fibs?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7257 Accepted Submission(s): 2842
Problem Description
Recall the definition of the Fibonacci numbers:
f1 := 1
f2 := 2
fn := fn-1 + fn-2 (n >= 3)
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b].
f1 := 1
f2 := 2
fn := fn-1 + fn-2 (n >= 3)
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b].
Input
The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a = b = 0. Otherwise, a <= b <= 10^100. The numbers a and b are given with no superfluous leading zeros.
Output
For each test case output on a single line the number of Fibonacci numbers fi with a <= fi <= b.
Sample Input
10 100 1234567890 9876543210 0 0
Sample Output
5 4
Source
import java.math.BigInteger; import java.util.Scanner; public class BigNumber{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int i,cnt; BigInteger []arr = new BigInteger[480]; BigInteger a,b; arr[0] = new BigInteger("1"); arr[1] = new BigInteger("2"); for(i=2; i<480; i++) arr[i]=arr[i-1].add(arr[i-2]); while(true){ a=input.nextBigInteger(); b=input.nextBigInteger(); if(a.equals(BigInteger.ZERO)&&b.equals(BigInteger.ZERO)) break; i=0; cnt=0; while(arr[i].compareTo(b)<=0){ if(arr[i].compareTo(a)>=0) cnt++; i++; } System.out.println(cnt); } } }