Code covered by the BSD License  

Highlights from
mlunit_2008a

image thumbnail
from mlunit_2008a by Christopher
A MATLAB unit test framework supporting new classdef files (r2008a)

Sieve
classdef Sieve
% SIEVE Is a simple class implementing the Sieve of Eratoshenes.

   properties
   end

   methods
       % find all primes up to number n
       function list = primes(self, n)
           if n < 2
               list = [];
               return;
           end
           
           list = [2 3:2:n];
           i = 1;
           
           while true
               i = i + 1;
               len = length(list);
               if i > len
                   return;
               end
               
               x = list(i);
               if x > sqrt(list(len))
                   return;
               end
               
               removals = [];
               for j = i+1:len
                   if mod(list(j), x) == 0
                       removals = [removals j];
                   end
               end
               
               list(removals) = [];
           end
       end          
   end
end 

Contact us at files@mathworks.com