Advertisement
Guest User

PI_By_Coprime

a guest
May 25th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define n 10000 // Number of iterations (pairs)
  5.  
  6. int gcd(int a, int b){
  7.     if (a == 0 || b == 0){
  8.        return 0;
  9.     }
  10.     if (a == b){
  11.         return a;
  12.     }
  13.     if (a > b){
  14.         return gcd(a-b, b);
  15.     }
  16.     return gcd(a, b-a);
  17. }
  18.  
  19. int main()
  20. {
  21.     int i, a, b, c;
  22.     double coprime, pi;
  23.     time_t t;
  24.  
  25.     srand((unsigned) time(&t));
  26.     coprime=0;
  27.  
  28.     for (i=0; i<n; i++){
  29.         a = rand(); b=rand(); c=gcd(a, b);
  30.         if (c==1){
  31.             coprime++;
  32.         }
  33.     }
  34.     pi=sqrt(6/(coprime/n));
  35.     printf("\n\nPI=%f\n", pi);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement