-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi.c
More file actions
44 lines (31 loc) · 807 Bytes
/
Copy pathpi.c
File metadata and controls
44 lines (31 loc) · 807 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
39
40
41
42
43
/*
This program computes the value of "pi".
Run time ./pi
Output should be something like:
Pi is 3.141593 <-- (output from pi.c)
29.7u 0.0s 0:29 99% 0+0k 0+0io 0pf+0w <-- (output from time command)
^ ^ ^
| | (elapsed real runtime used by our program)
| (CPU-seconds used by system on behalf of user program)
(CPU-seconds used directly by user program)
*/
/* This is the original serial code */
#include <stdio.h>
const long num_intervals = 1000000000;
double f( double a )
{
return (4.0 / (1.0 + a*a));
}
main()
{
double h, sum, x, pi;
long i;
h = 1.0 / (double)num_intervals;
sum = 0.0;
for (i = 0; i < num_intervals; i++) {
x = h * ((double)i + 0.5);
sum += f(x);
}
pi = h * sum;
printf( "Pi is %f\n", pi );
}