Question

# Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...

Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n.

Let’s estimate natural using n = 20.

```#include <chrono>
#include <iostream>
#include <mutex>
#include <random>
#include <utility>
#include <vector>

using namespace std;

constexpr long long size= 1000000;
mutex myMutex;

void sumUp(unsigned long long& sum, const vector<int>& val,
unsigned long long beg, unsigned long long end){
long long localSum = 0;
for (auto it= beg; it < end; ++it){
localSum+= val[it];
}
lock_guard<mutex> myLock(myMutex);
sum += localSum;
}

int main(){

cout << endl;

vector<int> randValues;
randValues.reserve(size);

mt19937 engine (0);
uniform_int_distribution<> uniformDist(1,10);
for ( long long i=0 ; i< size ; ++i)
randValues.push_back(uniformDist(engine));

unsigned long long sum= 0;
auto start = chrono::system_clock::now();

long long slice = size / threads;
int startIdx=0;
for (int i = 0; i < threads; ++i) {
cout << "Thread[" << i << "] - slice ["
<< startIdx << ":" << startIdx+slice-1 << "]" << endl;
t[i] = thread(sumUp, ref(sum), ref(randValues), startIdx, startIdx+slice-1);
startIdx += slice;
}

for (int i = 0; i < threads; ++i)
t[i].join();

chrono::duration<double> dur= chrono::system_clock::now() - start;
cout << "Time for addition " << dur.count() << " seconds" << endl;
cout << "Result: " << sum << endl;

cout << endl;

}```

`#include <iostream>`

`using` `namespace` `std;`

`// Function to return the sum of`

`// 1/1 + 1/2 + 1/3 + ..+ 1/n`

`class` `gfg`

`{`

`     `

`public` `:` `double` `sum(``int` `n)`

`{`

`    ``double` `i, s = 0.0;`

`    ``for` ```(i = 1; i <= n; i++)```

`    ````s = s + 1/i;```

`    ``return` `s;`

`}`

`};`

`// Driver code`

`int` `main()`

`{`

`    ``gfg g;`

`    ``int` ```n = 5;```

`    ``cout <<` `"Sum is "` `<< g.sum(n);`

`    ``return` `0;`

`}`