This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,14 @@
import std.stdio, std.random, std.math;
double pi(in int nthrows) {
int inside;
foreach (i; 0 .. nthrows)
if (hypot(uniform(0, 1.0), uniform(0, 1.0)) <= 1)
inside++;
return 4.0 * inside / nthrows;
}
void main() {
foreach (p; 1 .. 8)
writefln("%10s: %07f", 10 ^^ p, pi(10 ^^ p));
}

View file

@ -0,0 +1,10 @@
import std.stdio, std.random, std.math, std.algorithm, std.range;
bool isIn(int){ return hypot(uniform(0,1.0), uniform(0,1.0)) <= 1; }
double pi(in int n) { return 4.0 * count!isIn(iota(n)) / n; }
void main() {
foreach (p; 1 .. 8)
writefln("%10s: %07f", 10 ^^ p, pi(10 ^^ p));
}