求解一个数的最大质因数,比如20的最大质因数是5,然后要求是求2020330361020的最大质因数,求matlab的程序,可以运行快一点的那种
x=2020330361020;
a=factor(x);
b=max(a)
fprintf('最大质因数是%8.0f\n',b)
【最大质因数 matlab,试题 算法训练 最大质因数】https://mbd.baidu.com/ma/s/lDiqRxme
判断是否为质数的代码:
%%%%%%%%--The Range--%%%%%%%%
n_range=1000;
%%%%%%%%--Processing--%%%%%%%%
n_pn=zeros(1,n_range);
n_pn(2)=2;
for n=3:n_range
[a,index]=max(n_pn);
for m=2:index
if mod(n,n_pn(m))==0 && n_pn(m)~=0
n_pn(n)=0;
break;
elseif mod(n,n_pn(m))~=0 && n_pn(m)~=0
n_pn(n)=n;
end
end
end
n_pn(n_pn==0)=[];
%%%%%%%%--Printing--%%%%%%%%
disp(n_pn);