math.primes.twins: calculating twin primes.

db4
John Benediktsson 2012-04-19 15:14:31 -07:00
parent 7a15418cf4
commit 449814a387
2 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,13 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: tools.test ;
IN: math.primes.twins
{ { } } [ 3 twin-primes-upto ] unit-test
{ { V{ 3 5 } V{ 5 7 } V{ 11 13 } } } [ 13 twin-primes-upto ] unit-test
{ t } [ 3 5 twin-primes? ] unit-test
{ f } [ 2 4 twin-primes? ] unit-test
{ f } [ 3 7 twin-primes? ] unit-test

View File

@ -0,0 +1,13 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: combinators.short-circuit grouping kernel math
math.primes sequences ;
IN: math.primes.twins
: twin-primes-upto ( n -- seq )
primes-upto 2 <clumps> [ first2 - abs 2 = ] filter ;
: twin-primes? ( x y -- ? )
{ [ - abs 2 = ] [ nip prime? ] [ drop prime? ] } 2&& ;