Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
25
Task/Average-loop-length/Factor/average-loop-length.factor
Normal file
25
Task/Average-loop-length/Factor/average-loop-length.factor
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue