Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
39
Task/Average-loop-length/Pluto/average-loop-length.pluto
Normal file
39
Task/Average-loop-length/Pluto/average-loop-length.pluto
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
require "table2"
|
||||
local fmt = require "fmt"
|
||||
|
||||
local nmax <const> = 20
|
||||
|
||||
local function avg(n)
|
||||
local tests = 1e4
|
||||
local sum = 0
|
||||
for _ = 1, tests do
|
||||
local v = table.rep(nmax, false)
|
||||
local x = 1
|
||||
while !v[x] do
|
||||
v[x] = true
|
||||
sum += 1
|
||||
x = math.random(1, n)
|
||||
end
|
||||
end
|
||||
return sum / tests
|
||||
end
|
||||
|
||||
local function ana(n)
|
||||
if n < 2 then return 1 end
|
||||
local term = 1
|
||||
local sum = 1
|
||||
for i = n, 2, -1 do
|
||||
term *= (i - 1) / n
|
||||
sum += term
|
||||
end
|
||||
return sum
|
||||
end
|
||||
|
||||
print(" N average analytical (error)")
|
||||
print("=== ========= ============ =========")
|
||||
for n = 1, nmax do
|
||||
local a = avg(n)
|
||||
local b = ana(n)
|
||||
local e = math.abs(a - b) / b * 100
|
||||
fmt.print("%3d %9.4f %12.4f (%6.2f%%)", n, a, b, e)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue