Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Monte-Carlo-methods/D/monte-carlo-methods-1.d
Normal file
14
Task/Monte-Carlo-methods/D/monte-carlo-methods-1.d
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import std.stdio, std.random, std.math;
|
||||
|
||||
double pi(in uint nthrows) /*nothrow*/ @safe /*@nogc*/ {
|
||||
uint inside;
|
||||
foreach (immutable i; 0 .. nthrows)
|
||||
if (hypot(uniform01, uniform01) <= 1)
|
||||
inside++;
|
||||
return 4.0 * inside / nthrows;
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (immutable p; 1 .. 8)
|
||||
writefln("%10s: %07f", 10 ^^ p, pi(10 ^^ p));
|
||||
}
|
||||
9
Task/Monte-Carlo-methods/D/monte-carlo-methods-2.d
Normal file
9
Task/Monte-Carlo-methods/D/monte-carlo-methods-2.d
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
void main() {
|
||||
import std.stdio, std.random, std.math, std.algorithm, std.range;
|
||||
|
||||
immutable isIn = (int) => hypot(uniform01, uniform01) <= 1;
|
||||
immutable pi = (in int n) => 4.0 * n.iota.count!isIn / n;
|
||||
|
||||
foreach (immutable p; 1 .. 8)
|
||||
writefln("%10s: %07f", 10 ^^ p, pi(10 ^^ p));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue