import java.text.DecimalFormat;
public class Fib {
public static void main(String args[]) {
for (int i = 1; i <= 20; i++) {
System.out.println("[" + i + "] : " + fib(i) + " : " + new DecimalFormat("#.###").format(ratio(i)));
}
}
private static int fib(int n) {
if (n==1 || n==2) {
return 1;
} else {
return fib(n-1) + fib(n-2);
}
}
private static double ratio(int n) {
if (n > 1) {
return (double)fib(n-1)/(double)fib(n);
} else {
return 0;
}
}
}
No comments:
Post a Comment