I need to write a parallel program which is performs the operations below for each integer number in the range of 1 to 10000
and also:
Find out if the numbers are prime or square numbers.
Display the prime numbers in an array
Display the square numbers in a different array
program should be in C, I have example but how to make this work in parallel?
#include <iostream>
#define N 10000
using namespace std;
int main()
{
int i,j;
bool orFirst[N+1],
orSquare[N+1];
for (i=1;i<=N;i++)
{
orFirst=true;
orSquare=false;
}
orFirst[1]=false;
for (i=2;i*i<=N;i++)
if (orFirst==true)
for (j=i*i;j<=N;j+=i)
orFirst[j]=false;
for (i=1;i*i<=N;i++)
orSquare[i*i]=true;
cout<<"First No in range 1-10000:\n";
for (i=1;i<=N;i++)
if (orFirst==true)
cout<<i<<" ";
cout<<"\n\nSquare No in the range of 1-10000:\n";
for (i=1;i<=N;i++)
if (orSquare==true)
cout<<i<<" ";
cin.get();
return 0;
}
#include <cmath>
#include <iostream>
#define N 10000
using namespace std; i
int main()
{
int i,j,
square;
bool orFirst[N+1],
orSquare[N+1];
#pragma omp parallel for
for (i=1;i<=N;i++)
{
square=sqrt(i);
#pragma omp sections
{
#pragma omp section
{
orFirst=true;
for (j=2;j<=square;j++)
if (i%j==0)
{
orFirst=false;
break;
}
}
#pragma omp section
{
if (square*square==i)
orSquare=true;
else
orSquare=false;
}
}
}
orFirst[1]=false;
cout<<"First No in range 1-10000: ";
for (i=1;i<=N;i++)
if (orFirst==true)
cout<<i<<" ";
cout<<"\n\nSquare No in the range of 1-10000: ";
for (i=1;i<=N;i++)
if (orSquare==true)
cout<<i<<" ";
cin.get();
return 0;
}