public class Test {
public static void main(String[] args) {
// 有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前 20 項之和
// 定義第一個序列的2個數
double a = 2;
double b = 1;
double temp = 0;
double sum = 0;
// 循環20次
for (int i = 1; i <= 20; i++) {
// 計算商的和
sum += a / b;
// 交換位置得到第二個序列數
temp = a;
a = a + b;
b = temp;
}
// 打印
System.out.println(sum);
}
}
結果爲:32.66026079864164