📄 factor.m
字号:
function f = factor(n)
%FACTOR Prime factors.
% FACTOR(N) returns a vector containing the prime factors of N.
%
% This function uses the simple sieve approach. It may require large
% memory allocation if the number given is too big. Technically it is
% possible to improve this algorithm, allocating less memory for most
% cases and resulting in a faster execution time. However, it will still
% have problems in the worst case, so we choose to impose an upper bound
% on the input number and error out for n > 2^32.
%
% Class support for input N:
% float: double, single
%
% See also PRIMES, ISPRIME.
% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 1.14.4.2 $ $Date: 2004/07/05 17:01:58 $
if numel(n)~=1, error('MATLAB:factor:NonScalarInput','N must be a scalar.'); end
if (n < 0) || (floor(n) ~= n)
error('MATLAB:factor:InputNotPosInt', 'N must be a positive integer.');
end
if n > 2^32
error('MATLAB:factor:InputOutOfRange','The maximum value of n allowed is 2^32.');
end
if n < 4
f=n;
return
else
f = [];
end
p = primes(sqrt(n));
while n>1,
d = find(rem(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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -