]> gitweb.factorcode.org Git - factor.git/blob - basis/math/primes/primes.factor
Get rid of vector reallocation by preallocating it
[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 fry kernel math math.bitwise math.functions
4 math.order math.primes.erato math.primes.erato.private
5 math.primes.miller-rabin math.ranges literals random sequences sets
6 vectors ;
7 IN: math.primes
8
9 <PRIVATE
10
11 : look-in-bitmap ( n -- ? ) $[ 8999999 sieve ] marked-unsafe? ; inline
12
13 : (prime?) ( n -- ? )
14     dup 8999999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
15
16 ! In order not to reallocate large vectors, we compute the upper bound
17 ! of the number of primes in a given interval. We use a double inequality given
18 ! by Pierre Dusart in http://www.ams.org/mathscinet-getitem?mr=99d:11133
19 ! for x > 598. Under this limit, we know that there are at most 108 primes.
20 : upper-pi ( x -- y )
21     dup log [ / ] [ 1.2762 swap / 1 + ] bi * ceiling ;
22
23 : lower-pi ( x -- y )
24     dup log [ / ] [ 0.992 swap / 1 + ] bi * floor ;
25
26 : <primes-vector> ( low high -- vector )
27     swap [ [ upper-pi ] [ lower-pi ] bi* - >integer
28     108 max 10000 min <vector> ] keep
29     3 < [ [ 2 swap push ] keep ] when ;
30
31 PRIVATE>
32
33 : prime? ( n -- ? )
34     {
35         { [ dup 7 < ] [ { 2 3 5 } member? ] }
36         { [ dup even? ] [ 2 = ] }
37         [ (prime?) ]
38     } cond ; foldable
39
40 : next-prime ( n -- p )
41     dup 2 < [
42         drop 2
43     ] [
44         next-odd [ dup prime? ] [ 2 + ] until
45     ] if ; foldable
46
47 : primes-between ( low high -- seq )
48     [ [ 3 max dup even? [ 1 + ] when ] dip 2 <range> ]
49     [ <primes-vector> ] 2bi
50     [ '[ [ prime? ] _ push-if ] each ] keep clone ;
51
52 : primes-upto ( n -- seq ) 2 swap primes-between ;
53
54 : coprime? ( a b -- ? ) gcd nip 1 = ; foldable
55
56 : random-prime ( numbits -- p )
57     random-bits* next-prime ;
58
59 : estimated-primes ( m -- n )
60     dup log / ; foldable
61
62 ERROR: no-relative-prime n ;
63
64 <PRIVATE
65
66 : (find-relative-prime) ( n guess -- p )
67     over 1 <= [ over no-relative-prime ] when
68     dup 1 <= [ drop 3 ] when
69     2dup gcd nip 1 > [ 2 + (find-relative-prime) ] [ nip ] if ;
70
71 PRIVATE>
72
73 : find-relative-prime* ( n guess -- p )
74     #! find a prime relative to n with initial guess
75     >odd (find-relative-prime) ;
76
77 : find-relative-prime ( n -- p )
78     dup random find-relative-prime* ;
79
80 ERROR: too-few-primes n numbits ;
81
82 : unique-primes ( n numbits -- seq )
83     2dup 2^ estimated-primes > [ too-few-primes ] when
84     2dup [ random-prime ] curry replicate
85     dup all-unique? [ 2nip ] [ drop unique-primes ] if ;