Mercurial > octave-antonio
annotate scripts/specfun/isprime.m @ 10657:c6833d31f34e
optimize primes and isprime
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 25 May 2010 13:46:22 +0200 |
parents | d1cc2e0ddf55 |
children | faff5367cc05 |
rev | line source |
---|---|
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
1 ## Copyright (C) 2000, 2006, 2007, 2009 Paul Kienzle |
5827 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5827 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5827 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} isprime (@var{n}) | |
21 ## Return true if @var{n} is a prime number, false otherwise. | |
22 ## | |
23 ## Something like the following is much faster if you need to test a lot | |
24 ## of small numbers: | |
25 ## | |
26 ## @example | |
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
27 ## @var{t} = ismember (@var{n}, primes (max (@var{n} (:)))); |
5827 | 28 ## @end example |
29 ## | |
30 ## If max(n) is very large, then you should be using special purpose | |
31 ## factorization code. | |
32 ## | |
33 ## @seealso{primes, factor, gcd, lcm} | |
34 ## @end deftypefn | |
35 | |
36 function t = isprime (n) | |
7125 | 37 |
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
38 if (nargin == 1) |
10657
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
39 n = n(:); |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
40 idx = 1:numel (n); |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
41 for p = primes (sqrt (max (n(:)))) |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
42 if (isempty (idx)) |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
43 break; |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
44 endif |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
45 mask = rem (n, p) != 0; |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
46 n = n(mask); |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
47 idx = idx(mask); |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
48 endfor |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
49 t = false (size (n)); |
c6833d31f34e
optimize primes and isprime
Jaroslav Hajek <highegg@gmail.com>
parents:
9984
diff
changeset
|
50 t(idx) = true; |
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
51 else |
7125 | 52 print_usage (); |
53 endif | |
54 | |
5827 | 55 endfunction |
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
56 |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
57 %!assert (isprime (4), logical (0)); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
58 %!assert (isprime (3), logical (1)); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
59 %!assert (isprime (magic (3)), logical ([0, 0, 0; 1, 1, 1; 0, 0, 1])); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
60 %!error isprime () |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
61 %!error isprime (1, 2) |