> How do i make the following into a formula I can use in C++ code?

How do i make the following into a formula I can use in C++ code?

Posted at: 2014-12-18 
I see in your comment that you basically have it: You might facepalm when you see the solution.

Here.

# include

constexpr uint32_t SeriesLimit = 1e8;

int main (int, char **) {

? ? double SeriesSum = 0;

? ? for (uint32_t Denom = 1; Denom != SeriesLimit; Denom ++)

? ? ? ? SeriesLimit += (1.0 / static_cast (Denom));

? ? std:: cout << SeriesSum;

}

Printing the result gives

18.9979

A better way is probably to recognize this sum as the Harmonic Series, specifically you are asked for the 1e8'th harmonic number. The nth harmonic number is Euler's constant 0.5772156649 + ln (n) + o(1).

o(1) is indeterminate, but is small as n -> infinity, so as n is large, we get a good approximation with



0.5772156649 + ln (n):

0.5772156649 + 18.4206807 ~= 18.9978963649

take an long int i. for i=1; i<100000000;i+=1 sum=sum+1/i.

The key is to stop racking your brain and write code instead.

Edit:

You should initialize F1 to 0, not 1 and use double instead of float.

Then use

F1 += (1 / F2++);

in your loop.

The answer I got was 18.9979

I need to make a loop with the formula to get me the sum of the following numbers. I have to do it once using float and once using double variables. I have been racking my brains for the past 2 hours but cannot figure out how to set up this equation/formula in C++ code. Any suggestions? its due tonight so I will award best answer by tonight for whoever helps me solve this.

1/1 + 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/99999999 + 1/100000000