List the first 100 numbers and the ratio of
a number to its previous number, such as 1/1 = 1, 2/1 = 2, 3/2 = 1·5, 5/3 = 1·666..., 8/5 = 1·6, 13/8 = 1·625, 21/13 = 1·61538....
Want to know more about Fibonacci number
------
超過能列出來的數字就怪怪的...
就算改成LONG也只能列到第93項...
之後會出現1.618033988749895的規律
------
import java.util.Scanner;
public class Lab0330 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
long i, a=1,b=1,n,s;
System.out.println("請問要Fibonacci數列的第幾項?");
n = keyboard.nextInt();
for(i=1; i < n-1 ; i ++ ){
s=a+b;
b=a;
a=s;
double c=(double)a/b;
System.out.println(a+"/"+b+"="+c);
}
System.out.println("上列為後項除以前項之數列");
System.out.println("第"+n+"項為"+a);
}
}
改用LONG會在93項爆炸
回覆刪除為一收斂
回覆刪除