factor/extra/benchmark/beust2/beust2.factor

39 lines
1.1 KiB
Factor
Raw Permalink Normal View History

2008-07-14 01:49:09 -04:00
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2012-07-19 22:10:09 -04:00
USING: kernel locals math math.ranges math.parser sequences ;
2008-07-14 01:49:09 -04:00
IN: benchmark.beust2
2008-07-14 01:54:30 -04:00
! http://crazybob.org/BeustSequence.java.html
2008-08-24 04:59:22 -04:00
:: (count-numbers) ( remaining first value used max listener: ( -- ) -- ? )
10 first - <iota> [| i |
2009-10-27 22:50:31 -04:00
i first + :> digit
digit 2^ :> mask
i value + :> value'
used mask bitand zero? [
value max > [ t ] [
remaining 1 <= [
listener call f
] [
remaining 1 -
0
value' 10 *
used mask bitor
max
listener
(count-numbers)
2008-07-14 01:49:09 -04:00
] if
2009-10-27 22:50:31 -04:00
] if
] [ f ] if
] any? ; inline recursive
2008-07-14 01:49:09 -04:00
:: count-numbers ( max listener -- )
10 <iota> [ 1 + 1 1 0 max listener (count-numbers) ] any? drop ; inline
2008-07-14 01:49:09 -04:00
:: beust2-benchmark ( -- )
2009-10-27 22:50:31 -04:00
0 :> i!
5000000000 [ i 1 + i! ] count-numbers
2012-07-19 22:10:09 -04:00
i 7063290 assert= ;
2008-07-14 01:49:09 -04:00
MAIN: beust2-benchmark