|
发表于 2017-1-22 13:06:02
|
显示全部楼层
此代码使用matlab编程
Problem3所用时间为0.58204秒
Problem3的答案为6857
- %题目3:找出一个合数的最大质数因子
- function Output=Problem3(Input)
- tic
- if nargin==0
- Input=600851475143;
- end
- for ii=floor(sqrt(Input)):-1:2
- temp=mod(Input,ii);
- if temp==0
- if Prime_Judge(ii)==1
- Output=ii;
- break
- end
- end
- end
- toc
- disp('此代码使用matlab编程')
- disp(['Problem3所用时间为',num2str(toc),'秒'])
- disp(['Problem3的答案为',num2str(Output)])
- end
- %验证其是否为素数
- %若输入为素数则Judge为1,否则为0
- function Judge=Prime_Judge(Input)
- if mod(Input,2)==0
- Judge=0;
- elseif mod(Input,3)==0
- Judge=0;
- else
- node=floor(sqrt(Input))+1;
- Judge=1;
- for ii=2:node
- if mod(Input,ii)==0
- Judge=0;
- break
- else
- end
- end
- end
- end
复制代码 |
|