Mercurial > octave-nkf
annotate scripts/miscellaneous/list_primes.m @ 20614:10ec79b47808
use new string_value method to handle value extraction errors
* __voronoi__.cc, chol.cc, colamd.cc, fftw.cc:
Use new string_value method.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 08 Oct 2015 18:15:56 -0400 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19238
diff
changeset
|
1 ## Copyright (C) 1993-2015 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}) |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
22 ## List the first @var{n} primes. |
2311 | 23 ## |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
24 ## If @var{n} is unspecified, the first 25 primes are listed. |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## @seealso{primes, isprime} |
3449 | 26 ## @end deftypefn |
68 | 27 |
2314 | 28 ## Author: jwe |
29 | |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
30 function retval = list_primes (n = 25) |
68 | 31 |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
32 if (nargin > 1) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 print_usage (); |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
34 elseif (! isreal (n) || ! isscalar (n)) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 error ("list_primes: N must be a real scalar"); |
68 | 36 endif |
37 | |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
38 n = floor (n); |
68 | 39 |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 if (n < 1) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
41 retval = []; |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 return; |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 elseif (n == 1) |
68 | 44 retval = 2; |
45 return; | |
46 endif | |
47 | |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
48 list = primes (n * log (5 * n)); |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 if (numel (list) < n) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
50 ## Algorithm tested up to n=10,000 without failure. |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 error ("list_primes: Algorithm failed. Try primes (n*log (6*n))(1:n)"); |
68 | 52 endif |
53 | |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
54 retval = list(1:n); |
68 | 55 |
2326 | 56 endfunction |
12859
372eb47cd6a5
Wrote 1 test for list_primes.m
Giles Anderson <agander@gmail.com>
parents:
11523
diff
changeset
|
57 |
372eb47cd6a5
Wrote 1 test for list_primes.m
Giles Anderson <agander@gmail.com>
parents:
11523
diff
changeset
|
58 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
59 %!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
|
60 %! 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]) |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
61 %!assert (list_primes (5), [2, 3, 5, 7, 11]) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
62 |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
63 %!assert (list_primes (0), []) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
64 %!assert (list_primes (1), [2]) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
65 |
19238
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
66 ## Test input validation |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
67 %!error list_primes (1, 2) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
68 %!error <N must be a real scalar> list_primes (i) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 %!error <N must be a real scalar> list_primes ([1 2]) |
6d92d54046f3
list_primes.m: Overhaul function for 50X speed increase.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
70 |