function isp = isprime(X)
%ISPRIME True for prime numbers.
%   ISPRIME(X) is 1 for the elements of X which are prime, 0 otherwise.
%
%   See also FACTOR, PRIMES.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.12 $  $Date: 2000/06/01 15:14:03 $

if isempty(X), isp = logical(zeros(size(X))); return, end
if any(X(:) < 0) | any(floor(X(:)) ~= X(:))
  error('All entries of X must be positive integers.'); 
end

isp = logical(X);
n = max(X(:));
if n > 2^32
    error('The maximum value of X allowed is 2^32.');
end

p = primes(ceil(sqrt(n)));
for k = 1:prod(size(isp))
   isp(k) = all(rem(X(k), p(p<X(k))));
end

% p(p<1) would give an empty matrix and all([]) returns true.
% we need to correct isp for this case.
isp(X==1 | X==0)=0;
