0
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} primes (@var{n}) |
|
19 ## Return all primes up to @var{n}. |
|
20 ## |
|
21 ## Note that if you need a specific number of primes, you can use the |
|
22 ## fact the distance from one prime to the next is on average |
|
23 ## proportional to the logarithm of the prime. Integrating, you find |
|
24 ## that there are about @math{k} primes less than @math{k \log ( 5 k )}. |
|
25 ## |
|
26 ## The algorithm used is called the Sieve of Erastothenes. |
|
27 ## @end deftypefn |
|
28 |
|
29 ## Author: Paul Kienzle, Francesco Potort� and Dirk Laurie |
|
30 |
|
31 function x=primes(p) |
|
32 if nargin != 1 |
|
33 usage("p = primes(n)"); |
|
34 endif |
|
35 if (p > 100000) |
|
36 ## optimization: 1/6 less memory, and much faster (asymptotically) |
|
37 ## 100000 happens to be the cross-over point for Paul's machine; |
|
38 ## below this the more direct code below is faster. At the limit |
|
39 ## of memory in Paul's machine, this saves .7 seconds out of 7 for |
|
40 ## p=3e6. Hardly worthwhile, but Dirk reports better numbers. |
|
41 lenm = floor((p+1)/6); # length of the 6n-1 sieve |
|
42 lenp = floor((p-1)/6); # length of the 6n+1 sieve |
|
43 sievem = ones (1, lenm); # assume every number of form 6n-1 is prime |
|
44 sievep = ones (1, lenp); # assume every number of form 6n+1 is prime |
|
45 for i=1:(sqrt(p)+1)/6 # check up to sqrt(p) |
|
46 if (sievem(i)) # if i is prime, eliminate multiples of i |
|
47 sievem(7*i-1:6*i-1:lenm) = 0; |
|
48 sievep(5*i-1:6*i-1:lenp) = 0; |
|
49 endif # if i is prime, eliminate multiples of i |
|
50 if (sievep(i)) |
|
51 sievep(7*i+1:6*i+1:lenp) = 0; |
|
52 sievem(5*i+1:6*i+1:lenm) = 0; |
|
53 endif |
|
54 endfor |
|
55 x = sort([2, 3, 6*find(sievem)-1, 6*find(sievep)+1]); |
|
56 elseif (p > 352) # nothing magical about 352; just has to be greater than 2 |
|
57 len = floor((p-1)/2); # length of the sieve |
|
58 sieve = ones (1, len); # assume every odd number is prime |
|
59 for i=1:(sqrt(p)-1)/2 # check up to sqrt(p) |
|
60 if (sieve(i)) # if i is prime, eliminate multiples of i |
|
61 sieve(3*i+1:2*i+1:len) = 0; # do it |
|
62 endif |
|
63 endfor |
|
64 x = [2, 1+2*find(sieve)]; # primes remaining after sieve |
|
65 else |
|
66 a=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, \ |
|
67 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, \ |
|
68 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193,\ |
|
69 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269,\ |
|
70 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349]; |
|
71 x = x (x<=p); |
|
72 endif |
|
73 endfunction |