You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
698 B
C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main(int argc, char *argv[]) {
if(argc != 2) {
fprintf(stderr, "Uso: %s <tiros>\n", argv[0]);
return 1;
}
srand(time(NULL));
long tiros = atol(argv[1]);
long positivos = 0;
for(long i = 0; i < tiros; i++) {
double x = (double)rand() / RAND_MAX;
double y = (double)rand() / RAND_MAX;
if(x * x + y * y < 1)
positivos++;
}
double pi = 4.0 * positivos / tiros;
printf("Tiros = %ld, Positivos = %ld\n", tiros, positivos);
printf("π ~= %.15f\n", pi);
printf("ε = %f%%\n", 100 * fabs(M_PI - pi) / M_PI);
return 0;
}