Guest User

Untitled

a guest
Jun 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <time.h>
  2. int main() {
  3.  
  4.   /*Declare Variables*/
  5.   clock_t start, end, begin, stop;
  6.   double runTime, time;
  7.   start = clock();
  8.   int i,primes,num,count,total;
  9.  
  10.   /*Assign Variables*/
  11.   primes=0;
  12.   num=1;
  13.   begin = clock();
  14.  
  15.   /*Begin program, accept input*/
  16.   system("cls");
  17.   printf("Please enter the highest prime you want to generate to: ");
  18.   scanf("%d",&total);
  19.   printf("\n");
  20.  
  21.   /*Begin MATHS!*/
  22.   #pragma omp parallel for schedule(dynamic) reduction(+ : primes)
  23.   for (num = 1; num <= total; num++) {
  24.     i=2;
  25.  
  26.     while(i<=num) {
  27.       if(num%i==0)
  28.       break;
  29.       i++;
  30.     }
  31.  
  32.     if(i==num) {
  33.       primes++;
  34.     }
  35.  
  36.     num++;
  37.     stop = clock();
  38.     time = (stop - begin) / (double)CLOCKS_PER_SEC;
  39.  
  40.     if (time < 0.1) {
  41.       count++;  
  42.     }
  43.     else {
  44.       system("cls");
  45.       printf("Performing %d calculations per second; %d prime numbers calculated\n",count,primes);
  46.       begin = clock();
  47.       count=0;
  48.     }  
  49.  
  50.   }
  51.  
  52.   /*Print results*/
  53.   end = clock();
  54.   runTime = (end - start) / (double) CLOCKS_PER_SEC;
  55.   printf("This machine calculated all %d prime numbers under %d in %g seconds\n",primes,total,runTime);
  56.   return 0;
  57. }
Add Comment
Please, Sign In to add comment