Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //reference: https://gist.github.com/thinkphp/0d56dfd5eb5f91da029a91d4c7676f12
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <math.h>
- clock_t start, end;
- int main(int argc, char*argv[])
- {
- int n=atoi(argv[1]);
- double pi = 1.0;
- srand(time(NULL));
- int count=0;
- start = clock();
- for(int i=0;i<n;i++)
- {
- double x=(double)rand()/((double)RAND_MAX);
- double y=(double)rand()/((double)RAND_MAX);
- //x=-1+x*2;
- //y=-1+y*2;
- double distance= 1.0*x*x+1.0*y*y;
- if(distance<=1) count++;
- }
- pi=4.0*count/n;
- end = clock();
- double exe_time = ((double)(end - start)) / ( (double)(CLOCKS_PER_SEC) );
- printf("Pi: %lf\n",pi);
- printf("Time: %g seconds\n", exe_time);
- return 0;
- }
Add Comment
Please, Sign In to add comment