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
7 ERROR: invalid-lucas-lehmer-candidate obj ;
11 : do-lucas-lehmer ( p -- ? )
12 [ drop 4 ] [ 2 - ] [ 2^ 1 - ] tri
13 '[ sq 2 - _ mod ] times 0 = ;
15 : lucas-lehmer-guard ( obj -- obj )
16 dup { [ integer? ] [ 0 > ] } 1&&
17 [ throw-invalid-lucas-lehmer-candidate ] unless ;
21 : lucas-lehmer ( p -- ? )
24 { [ dup 2 = ] [ drop t ] }
25 { [ dup prime? ] [ do-lucas-lehmer ] }