1 ! Copyright (c) 2008-2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators kernel locals math math.functions math.ranges
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 { [ over 1 <= ] [ 2drop f ] }
30 { [ over even? ] [ drop 2 = ] }
34 : miller-rabin ( n -- ? ) 10 miller-rabin* ;