Mercurial > hg > octave-terminal
changeset 68:46807fe3db5b
[project @ 1993-08-19 02:50:52 by jwe]
Initial revision
author | jwe |
---|---|
date | Thu, 19 Aug 1993 02:50:52 +0000 |
parents | 2876864f1fdb |
children | d2d19bd98f60 |
files | scripts/miscellaneous/list_primes.m |
diffstat | 1 files changed, 62 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/list_primes.m @@ -0,0 +1,62 @@ +function retval = list_primes (n) + +# usage: list_primes (n) +# +# List the first n primes. If n is unspecified, the first 30 primes +# are listed. +# +# The algorithm used is from page 218 of the TeXbook. + + if (nargin > 0) + if (! is_scalar (n)) + error ("list_primes: argument must be a scalar"); + endif + endif + + if (nargin == 0) + n = 30; + endif + + if (n == 1) + retval = 2; + return; + endif + + if (n == 2) + retval = [2; 3]; + return; + endif + + retval = zeros (1, n); + retval (1) = 2; + retval (2) = 3; + + n = n - 2; + i = 3; + p = 5; + while (n > 0) + + is_prime = 1; + is_unknown = 1; + d = 3; + while (is_unknown) + a = fix (p / d); + if (a <= d) + is_unknown = 0; + endif + if (a * d == p) + is_prime = 0; + is_unknown = 0; + endif + d = d + 2; + endwhile + + if (is_prime) + retval (i++) = p; + n--; + endif + p = p + 2; + + endwhile + +endfunction \ No newline at end of file