]> gitweb.factorcode.org Git - factor.git/blob - basis/math/primes/primes.factor
math.primes: use fast-gcd in coprime? and replicate instead of make.
[factor.git] / basis / math / primes / primes.factor
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 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 ;
7 IN: math.primes
8
9 <PRIVATE
10
11 : look-in-bitmap ( n -- ? )
12     $[ 8999999 sieve ] marked-unsafe? ; inline
13
14 : (prime?) ( n -- ? )
15     dup 8999999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
16
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.
21 : upper-pi ( x -- y )
22     dup log [ / ] [ 1.2762 swap / 1 + ] bi * ceiling ;
23
24 : lower-pi ( x -- y )
25     dup log [ / ] [ 0.992 swap / 1 + ] bi * floor ;
26
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 ;
31
32 : simple? ( n -- ? ) { [ even? ] [ 3 divisor? ] [ 5 divisor? ] } 1|| ;
33
34 PRIVATE>
35
36 : prime? ( n -- ? )
37     {
38         { [ dup 7 < ] [ { 2 3 5 } member? ] }
39         { [ dup simple? ] [ drop f ] }
40         [ (prime?) ]
41     } cond ; foldable
42
43 : next-prime ( n -- p )
44     dup 2 < [
45         drop 2
46     ] [
47         next-odd [ dup prime? ] [ 2 + ] until
48     ] if ; foldable
49
50 <PRIVATE
51
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 ;
56
57 PRIVATE>
58
59 : primes-between ( low high -- seq )
60     [ ceiling >integer ] [ floor >integer ] bi*
61     {
62         { [ 2dup > ] [ 2drop V{ } clone ] }
63         { [ dup 2 = ] [ 2drop V{ 2 } clone ] }
64         { [ dup 2 < ] [ 2drop V{ } clone ] }
65         [ (primes-between) ]
66     } cond ;
67
68 : primes-upto ( n -- seq ) 2 swap primes-between ;
69
70 : nprimes ( n -- seq ) 2 swap [ [ next-prime ] keep ] replicate nip ;
71
72 : coprime? ( a b -- ? ) fast-gcd 1 = ; foldable
73
74 : random-prime ( numbits -- p )
75     [ ] [ 2^ ] [ random-bits* next-prime ] tri
76     2dup < [ 2drop random-prime ] [ 2nip ] if ;
77
78 : estimated-primes ( m -- n )
79     dup log / ; foldable
80
81 ERROR: no-relative-prime n ;
82
83 <PRIVATE
84
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 ;
89
90 PRIVATE>
91
92 : find-relative-prime* ( n guess -- p )
93     #! find a prime relative to n with initial guess
94     >odd (find-relative-prime) ;
95
96 : find-relative-prime ( n -- p )
97     dup random find-relative-prime* ;
98
99 ERROR: too-few-primes n numbits ;
100
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 ;