Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/list_primes.m @ 17341:f4772605aec3
debug.m: Fix spacing in @seealso macro (bug #39871)
* scripts/miscellaneous/debug.m: Remove extra spaces within @seealso macro.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 28 Aug 2013 11:07:03 -0700 |
parents | 5bc9b9cb4362 |
children | d63878346099 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12859
diff
changeset
|
1 ## Copyright (C) 1993-2012 John W. Eaton |
2313 | 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. | |
2313 | 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/>. | |
245 | 18 |
3449 | 19 ## -*- texinfo -*- |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9165
diff
changeset
|
20 ## @deftypefn {Function File} {} list_primes () |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9165
diff
changeset
|
21 ## @deftypefnx {Function File} {} list_primes (@var{n}) |
3449 | 22 ## List the first @var{n} primes. If @var{n} is unspecified, the first |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
23 ## 25 primes are listed. |
2311 | 24 ## |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## The algorithm used is from page 218 of the @TeX{}book. |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## @seealso{primes, isprime} |
3449 | 27 ## @end deftypefn |
68 | 28 |
2314 | 29 ## Author: jwe |
30 | |
2311 | 31 function retval = list_primes (n) |
68 | 32 |
33 if (nargin > 0) | |
4030 | 34 if (! isscalar (n)) |
68 | 35 error ("list_primes: argument must be a scalar"); |
36 endif | |
37 endif | |
38 | |
39 if (nargin == 0) | |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 n = 25; |
68 | 41 endif |
42 | |
43 if (n == 1) | |
44 retval = 2; | |
45 return; | |
46 endif | |
47 | |
48 if (n == 2) | |
49 retval = [2; 3]; | |
50 return; | |
51 endif | |
52 | |
53 retval = zeros (1, n); | |
14844
5bc9b9cb4362
maint: Use Octave coding conventions for cuddled parenthesis in retval assignments.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
54 retval(1) = 2; |
5bc9b9cb4362
maint: Use Octave coding conventions for cuddled parenthesis in retval assignments.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
55 retval(2) = 3; |
68 | 56 |
57 n = n - 2; | |
58 i = 3; | |
59 p = 5; | |
60 while (n > 0) | |
61 | |
62 is_prime = 1; | |
63 is_unknown = 1; | |
64 d = 3; | |
65 while (is_unknown) | |
66 a = fix (p / d); | |
67 if (a <= d) | |
68 is_unknown = 0; | |
69 endif | |
70 if (a * d == p) | |
71 is_prime = 0; | |
72 is_unknown = 0; | |
73 endif | |
74 d = d + 2; | |
75 endwhile | |
76 | |
77 if (is_prime) | |
14844
5bc9b9cb4362
maint: Use Octave coding conventions for cuddled parenthesis in retval assignments.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
78 retval(i++) = p; |
68 | 79 n--; |
80 endif | |
81 p = p + 2; | |
82 | |
83 endwhile | |
84 | |
2326 | 85 endfunction |
12859
372eb47cd6a5
Wrote 1 test for list_primes.m
Giles Anderson <agander@gmail.com>
parents:
11523
diff
changeset
|
86 |
372eb47cd6a5
Wrote 1 test for list_primes.m
Giles Anderson <agander@gmail.com>
parents:
11523
diff
changeset
|
87 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %!assert (list_primes (), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, ... |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %! 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %!assert (list_primes (5), [2, 3, 5, 7, 11]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 |