Average loop length: Difference between revisions
Content deleted Content added
Add Factor |
|||
Line 699:
19 5.154166 5.152196 +0.038%
20 5.298119 5.293585 +0.086%</pre>
=={{header|Factor}}==
The <code>loop-length</code> word is more or less a translation of the inner loop of C's <code>test</code> function.
{{works with|Factor|0.99 2020-01-23}}
<lang factor>USING: formatting fry io kernel locals math math.factorials
math.functions math.ranges random sequences ;
: (analytical) ( m n -- x )
[ drop factorial ] [ ^ /f ] [ - factorial / ] 2tri ;
: analytical ( n -- x )
dup [1,b] [ (analytical) ] with map-sum ;
: loop-length ( n -- x )
[ 0 0 1 [ 2dup bitand zero? ] ] dip
'[ [ 1 + ] 2dip bitor 1 _ random shift ] while 2drop ;
:: average-loop-length ( n #tests -- x )
0 #tests [ n loop-length + ] times #tests / ;
: stats ( n -- avg exp )
[ 1,000,000 average-loop-length ] [ analytical ] bi ;
: .line ( n -- )
dup stats 2dup / 1 - 100 *
"%2d %8.4f %8.4f %6.3f%%\n" printf ;
" n\tavg\texp.\tdiff\n-------------------------------" print
20 [1,b] [ .line ] each</lang>
{{out}}
<pre>
n avg exp. diff
-------------------------------
1 1.0000 1.0000 0.000%
2 1.4993 1.5000 -0.044%
3 1.8877 1.8889 -0.064%
4 2.2193 2.2188 0.023%
5 2.5099 2.5104 -0.021%
6 2.7728 2.7747 -0.068%
7 3.0165 3.0181 -0.056%
8 3.2442 3.2450 -0.026%
9 3.4574 3.4583 -0.027%
10 3.6622 3.6602 0.054%
11 3.8537 3.8524 0.033%
12 4.0365 4.0361 0.010%
13 4.2094 4.2123 -0.070%
14 4.3819 4.3820 -0.004%
15 4.5469 4.5458 0.023%
16 4.7028 4.7043 -0.031%
17 4.8571 4.8579 -0.016%
18 5.0049 5.0071 -0.043%
19 5.1519 5.1522 -0.005%
20 5.2927 5.2936 -0.017%
</pre>
=={{header|Go}}==
|