PDA

View Full Version : Quick question about harmonics



Element17
09-15-2013, 05:24 PM
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Write an expression* whose value* is the 8th harmonic number.

I have tried both of these:



(double)(1 + (1 / 2) + (1 / 3) + (1 / 4) + (1 / 5) + (1 / 6) + (1 / 7) + (1 / 8))

(double)1 + (1 / 2) + (1 / 3) + (1 / 4) + (1 / 5) + (1 / 6) + (1 / 7) + (1 / 8)

Both are wrong

Correct Answer that I found!


1.0+(1/2.0)+(1/3.0)+(1/4.0)+(1/5.0)+(1/6.0)+(1/7.0) + (1/8.0)

JennyCocoX
09-15-2013, 07:55 PM
I typed your question into google.com and even though I can not yet post the links of websites I DID find some relevance on yahoo answers that might be of interest :)

Element17
09-16-2013, 03:22 PM
I typed your question into google.com and even though I can not yet post the links of websites I DID find some relevance on yahoo answers that might be of interest :)


I've already done that and those are no help either. I've tried reading them and their solutions.

masterBB
09-16-2013, 05:34 PM
Both are wrong

What do you mean, both are wrong?

e: Just wrote this in browser:

Double HarmonicNumber(int Step)
{
return (Step > 1) ? HarmonicNumber(Step-1) + (1/Step) : 1;
}

Element17
09-16-2013, 06:09 PM
What do you mean, both are wrong?

e: Just wrote this in browser:

Double HarmonicNumber(int Step)
{
return (Step > 1) ? HarmonicNumber(Step-1) + (1/Step) : 1;
}


I have the thing called MyCodeLab and it asked for the 8th harmonicNumber and I type both of solutions I posted in and it said they were wrong.