今天吃饭时,想到1/2 + 1/3 + 1/6 = 1,于是想到分母从3,4开始,即1/3, 1/4, 第3个分母是前面两个分母的乘积,即 1/3 + 1/4 + ...... + 1/n,当n等于多少时,和为1呢?于是用代码去算:
int a = 3, b =4;
double sum = 0;
double counts = 10000000000000000000000d;
for(int c = 0; c < counts; c++) {
c = a * b;
b= c;
System.out.println("c = " + c + "||" + "b = " + b);
sum = (1/a) + (1/b) +(1/c);
System.out.println("sum = " + sum);
if(sum == 1) {
System.out.println("c = " + c);
break;
}
}
}
public static void main(String[] args) {
guessfun();
}
}
结果是一直运算也得不到,然后一查,居然是调和级数的引用。
本文出自 “一鑫” 博客,请务必保留此出处http://8013881.blog.51cto.com/8003881/1379943