Creating an Instance of the Class
myprimes Function
The code for myprimes is as follows:
function p = myprimes(n)
% MYPRIMES Returns the primes between 0 and n.
% P = MYPRIMES(N) Returns the primes between 0 and n.
% This file is used as an example for the MATLAB
% Builder for Java product.
% Copyright 2001-2006 The MathWorks, Inc.
if length(n) ~= 1
error('N must be a scalar');
end
if n < 2
p = zeros(1,0);
return
end
p = 1:2:n;
q = length(p);
p(1) = 2;
for k = 3:2:sqrt(n)
if p((k+1)/2)
p(((k*k+1)/2):k:q) = 0;
end
end
p = (p(p>0));
3-5
Komentarze do niniejszej Instrukcji