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 ## usage: isprime(n) |
|
18 ## Return true if n is a prime number, false otherwise. |
|
19 ## |
|
20 ## Something like the following is much faster if you need to test a lot |
|
21 ## of small numbers: |
|
22 ## t = ismember (n, primes (max (n (:)))); |
|
23 ## If max(n) is very large, then you should be using special purpose |
|
24 ## factorization code. |
|
25 ## |
|
26 ## See also: primes, factor, gcd, lcm |
|
27 |
|
28 function t = isprime(n) |
|
29 if !is_scalar(n) |
|
30 [nr, nc] = size(n); |
|
31 t = n; |
|
32 for i=1:nr |
|
33 for j=1:nc |
|
34 t(i,j) = isprime(t(i,j)); |
|
35 endfor |
|
36 endfor |
|
37 elseif (n != fix(n) || n < 2) |
|
38 t = 0; |
|
39 elseif n < 4 |
|
40 t = 1; |
|
41 else |
|
42 q = n./[2:sqrt(n)]; |
|
43 t = all (q != fix(q)); |
|
44 endif |
|
45 endfunction |