From octave-sources-request at bevo dot che dot wisc dot edu Tue Jun 27 07:57:49 2000 Subject: Re: primes.m From: Francesco Potorti` To: pkienzle at kienzle dot powernet dot co dot uk (Paul Kienzle) CC: octave-sources at bevo dot che dot wisc dot edu Date: Tue, 27 Jun 2000 14:56:27 +0200 Your algorithm, optimized. About three times faster, and needs half the memory :-) function x=primes(p) len = (p-1)/2; # length of the sieve sieve = ones (1, len); # assume every odd number is prime for i=1:(sqrt(p)/2) # check up to sqrt(p) if (sieve(i)) # if i is prime, eliminate multiples of i sieve(3*i+1:2*i+1:len) = 0; # do it endif endfor x = [2, 1+2*find(sieve)]; # primes remaining after sieve endfunction -- Francesco Potort́ (researcher) Voice: +39 050 315 3058 (op.2215) Area della ricerca CNR - CNUCE Fax: +39 050 3138091 via Vittorio Alfieri 1 Email: F dot Potorti at cnuce dot cnr dot it I-56010 Ghezzano, Pisa Web: http://fly.cnuce.cnr.it/ ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------