matlab nextpow,大数的素性检测(用MATLAB仿真)

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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.

% % Class support for input N:

% float: double, single

% integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64

%% See also PRIMES, ISPRIME.

% Copyright 1984-2012 The MathWorks, Inc.

% $Revision: 1.14.4.5 $ $Date: 2012/10/15 20:09:54 $

if ~isscalar(n)

rror(message('MATLAB:factor:NonScalarInput'));

end

if ~isreal(n) || (n < 0) || (floor(n) ~= n)

error(message('MATLAB:factor:InputNotPosInt'));

end

if (isfloat(n) && n > flintmax(class(n)))

error(message('MATLAB:factor:InputOutOfRange'));

end

if n < 4

f=n;

return

else

f = [];

end

if (isa(n,'uint64') || isa(n,'int64')) && n > flintmax

p = primes(2.^(nextpow2(n)/2));

else

p = primes(cast(sqrt(double(n)),class(n)));

end

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值