Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -1,10 +1,9 @@
|
|||
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; }
|
||||
enum isIn = (int) => hypot(uniform(0, 1.0), uniform(0, 1.0)) <= 1;
|
||||
enum pi = (in int n) => 4.0 * n.iota.count!isIn / n;
|
||||
|
||||
void main() {
|
||||
foreach (p; 1 .. 8)
|
||||
foreach (immutable p; 1 .. 8)
|
||||
writefln("%10s: %07f", 10 ^^ p, pi(10 ^^ p));
|
||||
}
|
||||
|
|
|
|||
10
Task/Monte-Carlo-methods/Maxima/monte-carlo-methods.maxima
Normal file
10
Task/Monte-Carlo-methods/Maxima/monte-carlo-methods.maxima
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
load(distrib);
|
||||
approx_pi(n):= block(
|
||||
[x: random_continuous_uniform(0, 1, n),
|
||||
y: random_continuous_uniform(0, 1, n),
|
||||
r, cin: 0],
|
||||
r: x^2 + y^2,
|
||||
for r0 in r do if r0<1 then cin: cin + 1,
|
||||
4*cin/n);
|
||||
|
||||
float(approx_pi(100));
|
||||
9
Task/Monte-Carlo-methods/Perl-6/monte-carlo-methods.pl6
Normal file
9
Task/Monte-Carlo-methods/Perl-6/monte-carlo-methods.pl6
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
my @random_distances := ([+] rand**2 xx 2) xx *;
|
||||
|
||||
sub approximate_pi(Int $n) {
|
||||
4 * @random_distances[^$n].grep(* < 1) / $n
|
||||
}
|
||||
|
||||
say "Monte-Carlo π approximation:";
|
||||
say "$_ iterations: ", approximate_pi $_
|
||||
for 100, 1_000, 10_000;
|
||||
Loading…
Add table
Add a link
Reference in a new issue