Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define n 10000 // Number of iterations (pairs)
- int gcd(int a, int b){
- if (a == 0 || b == 0){
- return 0;
- }
- if (a == b){
- return a;
- }
- if (a > b){
- return gcd(a-b, b);
- }
- return gcd(a, b-a);
- }
- int main()
- {
- int i, a, b, c;
- double coprime, pi;
- time_t t;
- srand((unsigned) time(&t));
- coprime=0;
- for (i=0; i<n; i++){
- a = rand(); b=rand(); c=gcd(a, b);
- if (c==1){
- coprime++;
- }
- }
- pi=sqrt(6/(coprime/n));
- printf("\n\nPI=%f\n", pi);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement