public class FractionAdd { public static void main(String[] args) { double sum = 0; double n1 = 1, n2 = 2, sn = 0;// 经分析,分子分母都为斐波那契数列 for (int i = 1; i <= 20; i++) {// 数列前20项,循环20次 sum += n2 / n1; sn = n1 + n2; n1 = n2; n2 = sn; } System.out.println("这个数列的前20项之和为:" + sum); } }
时间: 2024-10-24 15:37:36