调和级数Harmonic numbers,H(n)= 1/1 + 1/2 + 1/3 + 1/4 + ... + 1/n。求调和级数前n项的和
public static void main(String[] args) {
System.out.println(harmonic(15));
}
public static double harmonic (int n) {
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
return sum;
}