Name: Alexander Dubček 2015-03-15 6:33
Write a program in dc!
"monte carlo estimator for pi":
It needs a stream of floats:
Driver:
Example:
"monte carlo estimator for pi":
[? 2^ ? 2^ + 1>i]su
[lx 1+ sx]si
[lu x lm 1+ d sm ln>z]sz
5k
?sn
lzx
lx ln / 4* p
q
It needs a stream of floats:
/* frand.c */
#include <u.h>
#include <libc.h>
void
main(void)
{
srand(time(0));
for(;;)
print("%f\n", frand());
}
Driver:
#!/bin/rc
# runpi <number of samples>
{ echo $1; frand } | dc pi.dc
Example:
% runpi 10000
3.14840