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%%)",
|
||||
|
|
|
|||
14
Task/Average-loop-length/PARI-GP/average-loop-length.pari
Normal file
14
Task/Average-loop-length/PARI-GP/average-loop-length.pari
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
expected(n)=sum(i=1,n,n!/(n-i)!/n^i,0.);
|
||||
test(n, times)={
|
||||
my(ct);
|
||||
for(i=1,times,
|
||||
my(x=1,bits);
|
||||
while(!bitand(bits,x),ct++; bits=bitor(bits,x); x = 1<<random(n))
|
||||
);
|
||||
ct
|
||||
};
|
||||
TIMES=1000000;
|
||||
{for(n=1,20,
|
||||
my(cnt=test(n, TIMES),avg=cnt/TIMES,ex=expected(n),diff=(avg/ex-1)*100.);
|
||||
print(n"\t"avg*1."\t"ex*1."\t"diff);
|
||||
)}
|
||||
|
|
@ -6,7 +6,8 @@ end
|
|||
|
||||
def rand_until_rep(n)
|
||||
rands = {}
|
||||
loop do r = rand(1..n)
|
||||
loop do
|
||||
r = rand(1..n)
|
||||
return rands.size if rands[r]
|
||||
rands[r] = true
|
||||
end
|
||||
|
|
@ -14,10 +15,11 @@ end
|
|||
|
||||
runs = 1_000_000
|
||||
|
||||
puts " n\tavg\texp.\tdiff\n_____________________________"
|
||||
puts " N average exp. diff ",
|
||||
"=== ======== ======== ==========="
|
||||
(1..20).each do |n|
|
||||
sum_of_runs = runs.times.inject(0){|sum, _| sum += rand_until_rep(n)}
|
||||
avg = sum_of_runs / runs.to_f
|
||||
analytical = (1..n).inject(0){|sum, i| sum += (n.factorial / (n**i).to_f / (n-i).factorial)}
|
||||
puts "%2d %8.4f %8.4f %8.4f" % [n, avg, analytical, (avg/analytical - 1)*100]
|
||||
puts "%3d %8.4f %8.4f (%8.4f%%)" % [n, avg, analytical, (avg/analytical - 1)*100]
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue