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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6248
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6248
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6248
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6248
diff changeset
17 ## <http://www.gnu.org/licenses/>.
245
16a24e76d6e0 [project @ 1993-12-03 02:00:15 by jwe]
jwe
parents: 68
diff changeset
18
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 2847
diff changeset
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
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
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
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 2847
diff changeset
26 ## @end deftypefn
68
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
27
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
28 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
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
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
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
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
36 endif
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
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
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
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
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
44 retval = 2;
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
45 return;
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
46 endif
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
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
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
52 endif
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
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
46807fe3db5b [project @ 1993-08-19 02:50:52 by jwe]
jwe
parents:
diff changeset
55
2326
7ffb2a630708 [project @ 1996-07-15 22:31:34 by jwe]
jwe
parents: 2314
diff changeset
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