Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

dc

Name: Alexander Dubček 2015-03-15 6:33

Write a program in dc!

"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

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List