1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel fry math math.combinatorics math.order sequences
7 : count ( quot: ( -- ? ) -- n )
8 ! Call quot until it returns false, return number of times
10 [ 0 ] dip '[ _ dip swap [ [ 1 + ] when ] keep ] loop ; inline
12 : count-flips ( perm -- flip# )
15 [ 2drop f ] [ head-slice reverse! drop t ] if
18 : write-permutation ( perm -- )
19 [ CHAR: 0 + write1 ] each nl ; inline
21 : fannkuch-step ( counter max-flips perm -- counter max-flips )
22 pick 30 < [ [ 1 + ] [ ] [ dup write-permutation ] tri* ] when
23 count-flips max ; inline
27 [ 0 0 ] dip iota [ 1 + ] B{ } map-as
28 [ fannkuch-step ] each-permutation nip
30 "Pfannkuchen(" write pprint ") = " write . ;
32 : fannkuch-benchmark ( -- )
35 MAIN: fannkuch-benchmark