Hello everyone,

I'm trying to parallelize with the mpi library and c++ the internal loop of the following sequential code (sieve of eratosthenes):

Code:

int main()
{
int n = 40;
int root = std::sqrt(n);
int count = 0;
bool* range = new bool[n];
for (int i = 0; i < n; i++)
{
range[i] = true;
}
for (int i = 2; i <= root; i++)
{
if (range[i])
{
//Loop to be parallelized
for (int j = i * i; j < n; j += i)
{
range[j] = false;
}
}
}
for (int k = 2; k < n; k++)
{
if (range[k])
{
count++;
}
}
printf("Primes: %d", count);
}

I do not seek to parallelize the external loop, which although it would be a better solution, I must compare both performances.

In other words, I would like to parallelize the distribution of the elimination of multiples, which occurs when the range [j] takes the value of false

Thank you in advance.