-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcountpi.c
More file actions
38 lines (31 loc) · 725 Bytes
/
countpi.c
File metadata and controls
38 lines (31 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#define SQ(x) ((x)*(x))
/* Increase diameter to increase accuracy at cost of speed. */
#define DIAMETER 10000
int inCircle(int x, int y, int d)
{
/* (x-h)^2 + (y-k)^2 < r^2 */
/* in this case, h and k both equal r */
double r = (double)d / 2.0;
return SQ(x-r) + SQ(y-r) < SQ(r);
}
inline double estimatePi(double A, double d)
{
/* A = area; d = diameter */
/* pi = 4A / d^2 */
return 4.0*A / SQ(d);
}
int main(int argc, char *argv[])
{
int count = 0;
int x, y;
for (y=0; y<DIAMETER; y++)
for (x=0; x<DIAMETER; x++) {
if (inCircle(x, y, DIAMETER)) {
count ++;
}
}
double pi = estimatePi((double)count, (double)DIAMETER);
printf("Pi is approximately %.5lf.\n", pi);
return 0;
}