1 ! Copyright (C) 2007-2009 Samuel Tardieu.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators combinators.short-circuit fry kernel make math
4 math.bitwise math.functions math.order math.primes.erato
5 math.primes.erato.private math.primes.miller-rabin math.ranges
6 literals random sequences sets vectors ;
11 : look-in-bitmap ( n -- ? )
12 $[ 8999999 sieve ] marked-unsafe? ; inline
15 dup 8999999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
17 ! In order not to reallocate large vectors, we compute the upper bound
18 ! of the number of primes in a given interval. We use a double inequality given
19 ! by Pierre Dusart in http://www.ams.org/mathscinet-getitem?mr=99d:11133
20 ! for x > 598. Under this limit, we know that there are at most 108 primes.
22 dup log [ / ] [ 1.2762 swap / 1 + ] bi * ceiling ;
25 dup log [ / ] [ 0.992 swap / 1 + ] bi * floor ;
27 : <primes-vector> ( low high -- vector )
28 swap [ [ upper-pi ] [ lower-pi ] bi* - >integer
29 108 max 10000 min <vector> ] keep
30 3 < [ 2 suffix! ] when ;
32 : simple? ( n -- ? ) { [ even? ] [ 3 divisor? ] [ 5 divisor? ] } 1|| ;
38 { [ dup 7 < ] [ { 2 3 5 } member? ] }
39 { [ dup simple? ] [ drop f ] }
43 : next-prime ( n -- p )
47 next-odd [ dup prime? ] [ 2 + ] until
52 : (primes-between) ( low high -- seq )
53 [ [ 3 max dup even? [ 1 + ] when ] dip 2 <range> ]
54 [ <primes-vector> ] 2bi
55 [ '[ [ prime? ] _ push-if ] each ] keep clone ;
59 : primes-between ( low high -- seq )
60 [ ceiling >integer ] [ floor >integer ] bi*
62 { [ 2dup > ] [ 2drop V{ } clone ] }
63 { [ dup 2 = ] [ 2drop V{ 2 } clone ] }
64 { [ dup 2 < ] [ 2drop V{ } clone ] }
68 : primes-upto ( n -- seq ) 2 swap primes-between ;
70 : nprimes ( n -- seq ) [ 2 swap [ dup , next-prime ] times ] { } make nip ;
72 : coprime? ( a b -- ? ) gcd nip 1 = ; foldable
74 : random-prime ( numbits -- p )
75 [ ] [ 2^ ] [ random-bits* next-prime ] tri
76 2dup < [ 2drop random-prime ] [ 2nip ] if ;
78 : estimated-primes ( m -- n )
81 ERROR: no-relative-prime n ;
85 : (find-relative-prime) ( n guess -- p )
86 over 1 <= [ over no-relative-prime ] when
87 dup 1 <= [ drop 3 ] when
88 [ 2dup coprime? ] [ 2 + ] until nip ;
92 : find-relative-prime* ( n guess -- p )
93 #! find a prime relative to n with initial guess
94 >odd (find-relative-prime) ;
96 : find-relative-prime ( n -- p )
97 dup random find-relative-prime* ;
99 ERROR: too-few-primes n numbits ;
101 : unique-primes ( n numbits -- seq )
102 2dup 2^ estimated-primes > [ too-few-primes ] when
103 2dup [ random-prime ] curry replicate
104 dup all-unique? [ 2nip ] [ drop unique-primes ] if ;