Sunday, 8 September 2013

Calculates and returns the sum 1 + ½ + 1/3 + ... + 1/n

Calculates and returns the sum 1 + ½ + 1/3 + ... + 1/n

double harmonicSum(int n) {
if( n == 1 ) return 1.0;
else return 1.0/n + harmonicSum(n-1);
}

No comments:

Post a Comment