]> gitweb.factorcode.org Git - factor.git/blob - basis/math/primes/lucas-lehmer/lucas-lehmer.factor
basis: ERROR: changes.
[factor.git] / basis / math / primes / lucas-lehmer / lucas-lehmer.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators fry kernel locals math
4 math.primes combinators.short-circuit ;
5 IN: math.primes.lucas-lehmer
6
7 ERROR: invalid-lucas-lehmer-candidate obj ;
8
9 <PRIVATE
10
11 : do-lucas-lehmer ( p -- ? )
12     [ drop 4 ] [ 2 - ] [ 2^ 1 - ] tri
13     '[ sq 2 - _ mod ] times 0 = ;
14
15 : lucas-lehmer-guard ( obj -- obj )
16     dup { [ integer? ] [ 0 > ] } 1&&
17     [ throw-invalid-lucas-lehmer-candidate ] unless ;
18
19 PRIVATE>
20
21 : lucas-lehmer ( p -- ? )
22     lucas-lehmer-guard
23     {
24         { [ dup 2 = ] [ drop t ] }
25         { [ dup prime? ] [ do-lucas-lehmer ] }
26         [ drop f ]
27     } cond ;