Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an MPI program, count primes which will count the number of prime numbers

ID: 3707150 • Letter: W

Question

Write an MPI program, count primes which will count the number of prime numbers in the numbers from 1 to n inclusive where n is a long integer. The value for n which can be set in the program using a constant should be 50,000.

Each process will test its share of the cases.

Each process should print out any primes that it finds in a readable manner indicating which process found it and the actual prime.

The master process should end with printing a total for the count of the number of primes and the total amount of time taken to find all the primes.

In this program 1 is not a prime number. C Code

Explanation / Answer

here is your program : --------->>>>>>>>>>

#include<stdio.h>
#include<time.h>
#include<pthread.h>
#define SIZE 50000

long int count = 0;

void* myth(void *arg){
long int n = *((long int*)(arg));
if(n == 1 || n == 0){
  return;
}
int i;
for(i = 2;i<n;i++){
  if(n%i == 0){
   return;
  }
}
printf(" Process number %d Actual prime %d",n,n);
count++;
}

int main(){
pthread_t t[SIZE+1];
struct timeval start;
struct timeval end;
long int val[SIZE];
gettimeofday(&start,NULL);
int i = 1;
for(i;i<=SIZE;i++){
  val[i-1] = i;
  pthread_create(&t[i],NULL,myth,(void*)(&val[i-1]));
}

for(i = 0;i<=SIZE;i++){
  pthread_join(t[i],NULL);
}
gettimeofday(&end,NULL);
unsigned long st = start.tv_sec*1000000 + start.tv_usec;
unsigned long et = end.tv_sec*1000000 +end.tv_usec;
printf(" Total no. of Prime = %ld Total time = %ld micro seconds",count,(et-st));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote