A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 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 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));
|
||||
}
|
||||
10
Task/Monte-Carlo-methods/D/monte-carlo-methods-2.d
Normal file
10
Task/Monte-Carlo-methods/D/monte-carlo-methods-2.d
Normal 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));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue