Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.random, std.math, std.algorithm, std.range;
|
||||
|
||||
real analytical(in int n) /*pure nothrow*/ {
|
||||
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))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
MAXN = 20
|
||||
TIMES = 10000'00
|
||||
|
||||
't0=time$("ms")
|
||||
FOR n = 1 TO MAXN
|
||||
avg = FNtest(n, TIMES)
|
||||
theory = FNanalytical(n)
|
||||
diff = (avg / theory - 1) * 100
|
||||
PRINT n, avg, theory, using("##.####",diff); "%"
|
||||
NEXT
|
||||
't1=time$("ms")
|
||||
'print t1-t0; " ms"
|
||||
END
|
||||
|
||||
function FNanalytical(n)
|
||||
FOR i = 1 TO n
|
||||
s = s+ FNfactorial(n) / n^i / FNfactorial(n-i)
|
||||
NEXT
|
||||
FNanalytical = s
|
||||
end function
|
||||
|
||||
function FNtest(n, times)
|
||||
FOR i = 1 TO times
|
||||
x = 1 : b = 0
|
||||
WHILE (b AND x) = 0
|
||||
c = c + 1
|
||||
b = b OR x
|
||||
x = 2^int(n*RND(1))
|
||||
WEND
|
||||
NEXT
|
||||
FNtest = c / times
|
||||
end function
|
||||
|
||||
function FNfactorial(n)
|
||||
IF n=1 OR n=0 THEN FNfactorial=1 ELSE FNfactorial= n * FNfactorial(n-1)
|
||||
end function
|
||||
23
Task/Average-loop-length/Ruby/average-loop-length.rb
Normal file
23
Task/Average-loop-length/Ruby/average-loop-length.rb
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
class Integer
|
||||
def factorial
|
||||
self == 0 ? 1 : (1..self).inject(:*)
|
||||
end
|
||||
end
|
||||
|
||||
def rand_until_rep(n)
|
||||
rands = {}
|
||||
loop do r = rand(1..n)
|
||||
return rands.size if rands[r]
|
||||
rands[r] = true
|
||||
end
|
||||
end
|
||||
|
||||
runs = 1_000_000
|
||||
|
||||
puts " n\tavg\texp.\tdiff\n_____________________________"
|
||||
(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]
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue