Mercurial > hg > octave-lyh
annotate scripts/specfun/isprime.m @ 9984:d1cc2e0ddf55
isprime: produce logical result
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 14 Dec 2009 13:50:11 -0500 |
parents | f084ba47812b |
children | c6833d31f34e |
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) |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
39 if (! isscalar (n)) |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
40 nel = numel (n); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
41 t = zeros (size (n), "logical"); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
42 for i = 1:nel |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
43 t(i) = isprime (n(i)); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
44 endfor |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
45 elseif (n != fix (n) || n < 2) |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
46 t = logical (0); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
47 elseif (n < 9) |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
48 t = all (n != [4, 6, 8]); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
49 else |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
50 q = n./[2, 3:2:sqrt(n)]; |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
51 t = all (q != fix (q)); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
52 endif |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
53 else |
7125 | 54 print_usage (); |
55 endif | |
56 | |
5827 | 57 endfunction |
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
58 |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
59 %!assert (isprime (4), logical (0)); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
60 %!assert (isprime (3), logical (1)); |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
61 %!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
|
62 %!error isprime () |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
63 %!error isprime (1, 2) |