require "table2" local fmt = require "fmt" local nmax = 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