JAVA

Sunday, October 23, 2005

Fibonacci numbers

public class fb{

public static long fib(long n) {
if (n==1 || n==2)
return 1L;
else
return fib(n-1)+fib(n-2);
}

public static void main(String[] args){
int n;
double previous = 0, next = 0;
double ratio ;

for (n=1;n<=100;n++){
next = fib(n);
ratio = next/previous;
previous = next;
System.out.println(""+n+":"+fib(n));
System.out.println("ratio:"+ratio);
}


}
}

圖檔

0 Comments:

Post a Comment

<< Home