1/1 + 1/2 + 1/3 + 1/4 + ... 在数学上称为调和级数。
它是发散的,也就是说,只要加上足够多的项,就可以得到任意大的数字。
但是,它发散的很慢:
前1项和达到 1.0
前4项和才超过 2.0
前83项的和才超过 5.0
那么,请你计算一下,要加多少项,才能使得和达到或超过 15.0 呢?
请填写这个整数。
注意:只需要填写一个整数,不要填写任何多余的内容。比如说明文字。
public class searchIndex {
private double index = 0;
public double search() {
double i = 0;
double sum = 0;
for (i = 1.0; sum < 15.0; i++) {
sum += 1.0 / i;
index = i;
}
return index;
}
}
public class Main {
public static void main(String[] args) {
searchIndex sI = new searchIndex();
System.out.println((int)sI.search());
}
}