codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
// Print the value of pi #include <stdio.h> #include <stdlib.h> //#define const const int NUM_ITER= 8 * 1000 * 1000; const int RADIUS = 16384; int main() { const int SQRADIUS= RADIUS * RADIUS; int numHits = 0; int i = 0; srand(0); for(i = 0; i < NUM_ITER; ++i) { int x = 0, y= 0; x = rand() & (RADIUS-1); y = rand() & (RADIUS-1); x *= x; y *= y; numHits += (x + y < SQRADIUS) + (x + y <= SQRADIUS); } printf("Value of pi is: %g\n", 2.0 * numHits / NUM_ITER); return 0; }
Private
[
?
]
Run code
Submit