math.primes.solovay-strassen: adding Solovay-Strassen primality test.

db4
John Benediktsson 2014-12-13 16:52:28 -08:00
parent 1030f592e0
commit 24263299fa
3 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -0,0 +1,7 @@
USING: kernel math.primes sequences tools.test ;
IN: math.primes.solovay-strassen
{ t } [
100,000 iota [ solovay-strassen ] filter
100,000 primes-upto =
] unit-test

View File

@ -0,0 +1,30 @@
! Copyright (C) 2014 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: combinators kernel locals math math.extras
math.extras.private math.functions math.ranges random sequences ;
IN: math.primes.solovay-strassen
<PRIVATE
:: (solovay-strassen) ( n numtrials -- ? )
numtrials iota [
drop
n 1 - [1,b) random :> a
a n fast-gcd 1 > [ t ] [
a n jacobi n mod'
a n 1 - 2 /i n ^mod = not
] if
] any? not ;
PRIVATE>
: solovay-strassen* ( n numtrials -- ? )
{
{ [ over 1 <= ] [ 2drop f ] }
{ [ over even? ] [ drop 2 = ] }
[ (solovay-strassen) ]
} cond ;
: solovay-strassen ( n -- ? ) 32 solovay-strassen* ;