]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/beust2/beust2.factor
factor: trim using lists
[factor.git] / extra / benchmark / beust2 / beust2.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences ;
4 IN: benchmark.beust2
5
6 ! http://crazybob.org/BeustSequence.java.html
7
8 :: (count-numbers) ( remaining first value used max listener: ( -- ) -- ? )
9     10 first - <iota> [| i |
10         i first + :> digit
11         digit 2^ :> mask
12         i value + :> value'
13         used mask bitand zero? [
14             value max > [ t ] [
15                 remaining 1 <= [
16                     listener call f
17                 ] [
18                     remaining 1 -
19                     0
20                     value' 10 *
21                     used mask bitor
22                     max
23                     listener
24                     (count-numbers)
25                 ] if
26             ] if
27         ] [ f ] if
28     ] any? ; inline recursive
29
30 :: count-numbers ( max listener -- )
31     10 <iota> [ 1 + 1 1 0 max listener (count-numbers) ] any? drop ; inline
32
33 :: beust2-benchmark ( -- )
34     0 :> i!
35     5000000000 [ i 1 + i! ] count-numbers
36     i 7063290 assert= ;
37
38 MAIN: beust2-benchmark