1 ! Copyright (c) 2008-2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators combinators.short-circuit kernel locals math
4 math.functions math.ranges random sequences sets ;
5 IN: math.primes.miller-rabin
9 :: (miller-rabin) ( n trials -- ? )
11 n-1 factor-2s :> ( r s )
15 2 n 2 - [a,b] random a!
20 2^ s * a swap n ^mod n - -1 =
27 : miller-rabin* ( n numtrials -- ? )
29 { [ dup 1 <= ] [ 3drop f ] }
30 { [ dup 2 = ] [ 3drop t ] }
31 { [ dup even? ] [ 3drop f ] }
32 [ drop (miller-rabin) ]
35 : miller-rabin ( n -- ? ) 10 miller-rabin* ;