SRL Archive - Java Help and Tutorials

Quick question about harmonics


Element17

2013-09-15 13:24:25

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

2013-09-15 15:55:06

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

2013-09-16 11:22:38

From: JennyCocoX
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

2013-09-16 13:34:25

From: Element17
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

2013-09-16 14:09:39

From: masterBB
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.