minh_tran_782

pi_serial.c

Feb 25th, 2022 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. //reference: https://gist.github.com/thinkphp/0d56dfd5eb5f91da029a91d4c7676f12
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <math.h>
  6.  clock_t start, end;
  7. int main(int argc, char*argv[])
  8. {  
  9.    
  10.     int n=atoi(argv[1]);
  11.     double pi = 1.0;
  12.     srand(time(NULL));
  13.     int count=0;
  14.     start = clock();
  15.     for(int i=0;i<n;i++)
  16.     {
  17.         double x=(double)rand()/((double)RAND_MAX);
  18.         double y=(double)rand()/((double)RAND_MAX);
  19.         //x=-1+x*2;
  20.         //y=-1+y*2;
  21.         double distance= 1.0*x*x+1.0*y*y;
  22.         if(distance<=1) count++;
  23.     }
  24.     pi=4.0*count/n;
  25.     end = clock();
  26.     double exe_time = ((double)(end - start)) / ( (double)(CLOCKS_PER_SEC) );
  27.     printf("Pi: %lf\n",pi);
  28.     printf("Time: %g seconds\n", exe_time);
  29.     return 0;
  30. }
  31.  
Add Comment
Please, Sign In to add comment