function f = factor(n)
%FACTOR Prime factors.
%   FACTOR(N) returns a vector containing the prime factors of N.
%
%   See also PRIMES, ISPRIME.

%   Copyright (c) 1984-96 by The MathWorks, Inc.
%   $Revision: 1.5 $  $Date: 1996/03/13 00:06:49 $

if prod(size(n))~=1, error('N must be a scalar.'); end
if (n < 0) | (floor(n) ~= n), error('N must be a positive integer.'); end

f = 1;
if n < 4, f=n; return, end

p = primes(floor(sqrt(n)));
if isempty(p), error('N must be a positive integer.'); end
while n>1,
  d = find(mod(n,p)==0);
  if isempty(d)
    f = [f n];
    break;
  end
  p = p(d);
  f = [f p];
  n = n/prod(p);
end
f = sort(f);
f = f(2:end);
