Mercurial > hg > octave-lyh
comparison 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 |
comparison
equal
deleted
inserted
replaced
9983:2d347a2f4a0a | 9984:d1cc2e0ddf55 |
---|---|
1 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle | 1 ## Copyright (C) 2000, 2006, 2007, 2009 Paul Kienzle |
2 ## | 2 ## |
3 ## This file is part of Octave. | 3 ## This file is part of Octave. |
4 ## | 4 ## |
5 ## Octave is free software; you can redistribute it and/or modify it | 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 | 6 ## under the terms of the GNU General Public License as published by |
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} isprime (@var{n}) | 20 ## @deftypefn {Function File} {} isprime (@var{n}) |
21 ## | |
22 ## Return true if @var{n} is a prime number, false otherwise. | 21 ## Return true if @var{n} is a prime number, false otherwise. |
23 ## | 22 ## |
24 ## Something like the following is much faster if you need to test a lot | 23 ## Something like the following is much faster if you need to test a lot |
25 ## of small numbers: | 24 ## of small numbers: |
26 ## | 25 ## |
27 ## @example | 26 ## @example |
28 ## @var{t} = ismember (@var{n}, primes (max (@var{n} (:)))); | 27 ## @var{t} = ismember (@var{n}, primes (max (@var{n} (:)))); |
29 ## @end example | 28 ## @end example |
30 ## | 29 ## |
31 ## If max(n) is very large, then you should be using special purpose | 30 ## If max(n) is very large, then you should be using special purpose |
32 ## factorization code. | 31 ## factorization code. |
33 ## | 32 ## |
34 ## @seealso{primes, factor, gcd, lcm} | 33 ## @seealso{primes, factor, gcd, lcm} |
35 ## @end deftypefn | 34 ## @end deftypefn |
36 | 35 |
37 function t = isprime (n) | 36 function t = isprime (n) |
38 | 37 |
39 if (nargin < 1) | 38 if (nargin == 1) |
39 if (! isscalar (n)) | |
40 nel = numel (n); | |
41 t = zeros (size (n), "logical"); | |
42 for i = 1:nel | |
43 t(i) = isprime (n(i)); | |
44 endfor | |
45 elseif (n != fix (n) || n < 2) | |
46 t = logical (0); | |
47 elseif (n < 9) | |
48 t = all (n != [4, 6, 8]); | |
49 else | |
50 q = n./[2, 3:2:sqrt(n)]; | |
51 t = all (q != fix (q)); | |
52 endif | |
53 else | |
40 print_usage (); | 54 print_usage (); |
41 endif | 55 endif |
42 | 56 |
43 if (! isscalar (n)) | |
44 nel = numel (n); | |
45 t = n; | |
46 for i = 1:nel | |
47 t(i) = isprime (t(i)); | |
48 endfor | |
49 elseif (n != fix (n) || n < 2) | |
50 t = 0; | |
51 elseif (n < 9) | |
52 t = all (n != [4, 6, 8]); | |
53 else | |
54 q = n./[2, 3:2:sqrt(n)]; | |
55 t = all (q != fix (q)); | |
56 endif | |
57 endfunction | 57 endfunction |
58 | |
59 %!assert (isprime (4), logical (0)); | |
60 %!assert (isprime (3), logical (1)); | |
61 %!assert (isprime (magic (3)), logical ([0, 0, 0; 1, 1, 1; 0, 0, 1])); | |
62 %!error isprime () | |
63 %!error isprime (1, 2) |