Mercurial > octave
annotate scripts/specfun/isprime.m @ 29359:7854d5752dd2
maint: merge stable to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Feb 2021 10:10:40 -0500 |
parents | 90fea9cc9caa 0a5b15007766 |
children | a49c635b179d |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
3 ## Copyright (C) 2000-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27803
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5827 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5827 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
5827 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
5827 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5827 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
27 ## @deftypefn {} {} isprime (@var{x}) |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## Return a logical array which is true where the elements of @var{x} are prime |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
29 ## numbers and false where they are not. |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
30 ## |
19052
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19050
diff
changeset
|
31 ## A prime number is conventionally defined as a positive integer greater than |
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19050
diff
changeset
|
32 ## 1 (e.g., 2, 3, @dots{}) which is divisible only by itself and 1. Octave |
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19050
diff
changeset
|
33 ## extends this definition to include both negative integers and complex |
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19050
diff
changeset
|
34 ## values. A negative integer is prime if its positive counterpart is prime. |
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19050
diff
changeset
|
35 ## This is equivalent to @code{isprime (abs (x))}. |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19052
diff
changeset
|
36 ## |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
37 ## If @code{class (@var{x})} is complex, then primality is tested in the domain |
25143
13fd0610480f
doc: Use https whenever possible in @url entries.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
38 ## of Gaussian integers (@url{https://en.wikipedia.org/wiki/Gaussian_integer}). |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
39 ## Some non-complex integers are prime in the ordinary sense, but not in the |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
40 ## domain of Gaussian integers. For example, @math{5 = (1+2i)*(1-2i)} shows |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
41 ## that 5 is not prime because it has a factor other than itself and 1. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
42 ## Exercise caution when testing complex and real values together in the same |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
43 ## matrix. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
44 ## |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
45 ## Examples: |
5827 | 46 ## |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
47 ## @example |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
48 ## @group |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
49 ## isprime (1:6) |
26600
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
50 ## @result{} 0 1 1 0 1 0 |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
51 ## @end group |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
52 ## @end example |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
53 ## |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
54 ## @example |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
55 ## @group |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
56 ## isprime ([i, 2, 3, 5]) |
26600
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
57 ## @result{} 0 0 1 0 |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
58 ## @end group |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
59 ## @end example |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
60 ## |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
61 ## Programming Note: @code{isprime} is appropriate if the maximum value in |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
62 ## @var{x} is not too large (< 1e15). For larger values special purpose |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
63 ## factorization code should be used. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
64 ## |
27803
e8823425a60b
doc: Change markup for "Matlab".
Markus Mützel <markus.muetzel@gmx.de>
parents:
26600
diff
changeset
|
65 ## Compatibility Note: @sc{matlab} does not extend the definition of prime |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
66 ## numbers and will produce an error if given negative or complex inputs. |
5827 | 67 ## @seealso{primes, factor, gcd, lcm} |
68 ## @end deftypefn | |
69 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11431
diff
changeset
|
70 function t = isprime (x) |
7125 | 71 |
28891
de5f2f9a64ff
maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
72 if (nargin < 1) |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
73 print_usage (); |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
74 elseif (any (fix (x) != x)) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
75 error ("isprime: X contains non-integer entries"); |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
76 endif |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
77 |
19045
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19041
diff
changeset
|
78 if (isempty (x)) |
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19041
diff
changeset
|
79 t = x; |
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19041
diff
changeset
|
80 return; |
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19041
diff
changeset
|
81 endif |
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19041
diff
changeset
|
82 |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
83 if (iscomplex (x)) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
84 t = isgaussianprime (x); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
85 return; |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
86 endif |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
87 |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
88 ## Code strategy is to build a table with the list of possible primes |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
89 ## and then quickly compare entries in x with the table of primes using |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
90 ## lookup(). The table size is limited to save memory and computation |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
91 ## time during its creation. All entries larger than the maximum in the |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
92 ## table are checked by straightforward division. |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
93 |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
94 x = abs (x); # handle negative entries |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
95 maxn = max (x(:)); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
96 ## generate prime table of suitable length. |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
97 ## 1e7 threshold requires ~0.15 seconds of computation, 1e8 requires 1.8. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
98 maxp = min (maxn, max (sqrt (maxn), 1e7)); |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
99 pr = primes (maxp); |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
100 t = lookup (pr, x, "b"); # quick search for table matches. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
101 |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
102 ## process any remaining large entries |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
103 m = x(x > maxp); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
104 if (! isempty (m)) |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 if (maxn <= intmax ("uint32")) |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
106 m = uint32 (m); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 elseif (maxn <= intmax ("uint64")) |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
108 m = uint64 (m); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
109 else |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 warning ("isprime: X contains integers too large to be tested"); |
10664
faff5367cc05
second isprime rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10657
diff
changeset
|
111 endif |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
112 |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
113 ## Start by dividing through by the small primes until the remaining |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
114 ## list of entries is small (and most likely prime themselves). |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 pr = cast (pr(pr <= sqrt (maxn)), class (m)); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
116 for p = pr |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 m = m(rem (m, p) != 0); |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
118 if (numel (m) < numel (pr) / 10) |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
119 break; |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
120 endif |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 endfor |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
122 |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
123 ## Check the remaining list of possible primes against the |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
124 ## remaining prime factors which were not tested in the for loop. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
125 ## This is just an optimization to use arrayfun over for loo |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
126 pr = pr(pr > p); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
127 mm = arrayfun (@(x) all (rem (x, pr)), m); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 m = m(mm); |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
129 |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
130 ## Add any remaining entries, which are truly prime, to the results. |
10664
faff5367cc05
second isprime rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10657
diff
changeset
|
131 if (! isempty (m)) |
19041
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
132 m = cast (sort (m), class (x)); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
133 t |= lookup (m, x, "b"); |
10664
faff5367cc05
second isprime rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10657
diff
changeset
|
134 endif |
7125 | 135 endif |
136 | |
5827 | 137 endfunction |
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
138 |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
139 function t = isgaussianprime (z) |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
140 |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
141 ## Assume prime unless proven otherwise |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
142 t = true (size (z)); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
143 |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
144 x = real (z); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
145 y = imag (z); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
146 |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
147 ## If purely real or purely imaginary, ordinary prime test for |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
148 ## that complex part if that part is 3 mod 4. |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
149 xidx = y==0 & mod (x, 4) == 3; |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
150 yidx = x==0 & mod (y, 4) == 3; |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
151 |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
152 t(xidx) &= isprime (x(xidx)); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
153 t(yidx) &= isprime (y(yidx)); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
154 |
19050
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
155 ## Otherwise, prime if x^2 + y^2 is prime |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
156 zidx = ! (xidx | yidx); # Skip entries that were already evaluated |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
157 zabs = x(zidx).^2 + y(zidx).^2; |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19046
diff
changeset
|
158 t(zidx) &= isprime (zabs); |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
159 |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
160 endfunction |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
161 |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
162 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
163 %!assert (isprime (3), true) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
164 %!assert (isprime (4), false) |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
165 %!assert (isprime (5i), false) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
166 %!assert (isprime (7i), true) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
167 %!assert (isprime ([1+2i, (2+3i)*(-1+2i)]), [true, false]) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
168 %!assert (isprime (-2), true) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
169 %!assert (isprime (complex (-2)), false) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
170 %!assert (isprime (2i), false) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
171 %!assert (isprime ([i, 2, 3, 5]), [false, false, true, false]) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
172 %!assert (isprime (0), false) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
173 %!assert (isprime (magic (3)), logical ([0, 0, 0; 1, 1, 1; 0, 0, 1])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
174 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
175 ## Test input validation |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28891
diff
changeset
|
176 %!error <Invalid call> isprime () |
19046
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
177 %!error <X contains non-integer entries> isprime (0.5i) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19045
diff
changeset
|
178 %!error <X contains non-integer entries> isprime (0.5) |