Average loop length: Difference between revisions

Added 11l
m (→‎{{header|Nim}}: Fix broken markup)
(Added 11l)
Line 36:
20 5.2699 5.2936 ( 0.45%)</pre>
<br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F ffactorial(n)
V result = 1.0
L(i) 2..n
result *= i
R result
 
V MAX_N = 20
V TIMES = 1000000
 
F analytical(n)
R sum((1..n).map(i -> ffactorial(@n) / pow(Float(@n), Float(i)) / ffactorial(@n - i)))
 
F test(n, times)
V count = 0
L(i) 0 .< times
V (x, bits) = (1, 0)
L (bits [&] x) == 0
count++
bits [|]= x
x = 1 << random:(n)
R Float(count) / times
 
print(" n avg exp. diff\n-------------------------------")
L(n) 1 .. MAX_N
V avg = test(n, TIMES)
V theory = analytical(n)
V diff = (avg / theory - 1) * 100
print(‘#2 #3.4 #3.4 #2.3%’.format(n, avg, theory, diff))</lang>
 
{{out}}
<pre>
n avg exp. diff
-------------------------------
1 1.0000 1.0000 0.000%
2 1.5003 1.5000 0.022%
3 1.8897 1.8889 0.044%
4 2.2170 2.2187 -0.080%
5 2.5099 2.5104 -0.022%
6 2.7736 2.7747 -0.040%
7 3.0182 3.0181 0.001%
8 3.2438 3.2450 -0.037%
9 3.4589 3.4583 0.018%
10 3.6605 3.6602 0.008%
11 3.8517 3.8524 -0.017%
12 4.0373 4.0361 0.032%
13 4.2159 4.2123 0.085%
14 4.3828 4.3820 0.017%
15 4.5465 4.5458 0.016%
16 4.7048 4.7043 0.013%
17 4.8585 4.8579 0.012%
18 5.0042 5.0071 -0.057%
19 5.1465 5.1522 -0.110%
20 5.2907 5.2936 -0.054%
</pre>
 
=={{header|Ada}}==
1,481

edits