March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,17 +1,14 @@
|
|||
import std.stdio, std.random, std.math, std.algorithm, std.range;
|
||||
|
||||
real analytical(in int n) pure nothrow {
|
||||
real total = 0.0;
|
||||
foreach (immutable k; 1 .. n + 1) {
|
||||
immutable x = reduce!q{a * b}(1.0L, iota(n - k + 1, n + 1))
|
||||
* k ^^ 2;
|
||||
total += x / ((cast(real)n) ^^ (k + 1));
|
||||
}
|
||||
return total;
|
||||
real analytical(in int n) /*pure nothrow*/ {
|
||||
enum aux = (int k)=> reduce!q{a * b}(1.0L, iota(n - k + 1, n + 1));
|
||||
return iota(1, n + 1)
|
||||
.map!(k => (aux(k) * k ^^ 2) / (real(n) ^^ (k + 1)))
|
||||
.sum;
|
||||
}
|
||||
|
||||
size_t loopLength(size_t maxN)(in int size, ref Xorshift rng) {
|
||||
__gshared bool[maxN + 1] seen;
|
||||
__gshared static bool[maxN + 1] seen;
|
||||
seen[0 .. size + 1] = false;
|
||||
int current = 1;
|
||||
size_t steps = 0;
|
||||
|
|
@ -34,8 +31,8 @@ void main() {
|
|||
long total = 0;
|
||||
foreach (immutable _; 0 .. nTrials)
|
||||
total += loopLength!maxN(n, rng);
|
||||
immutable average = total / cast(real)nTrials;
|
||||
immutable an = analytical(n);
|
||||
immutable average = total / real(nTrials);
|
||||
immutable an = n.analytical;
|
||||
immutable percentError = abs(an - average) / an * 100;
|
||||
immutable errorS = format("%2.4f", percentError);
|
||||
writefln("%3d %9.5f %12.5f (%7s%%)",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue