|
发表于 2017-1-21 23:43:43
|
显示全部楼层
此代码使用matlab编程
Problem23所用时间为4.2954秒
Problem23的答案为4179871
- %% Problem23
- % 题目23:算出两个不能够被两个过剩数之和表示的所有正整数之和
- function Output=Problem23(Input)
- tic
- if nargin==0
- Input=28123;
- end
- Abundant=[];
- for ii=2:Input
- if sum(Factor_Rank(ii))>ii
- Temp=[Abundant,ii];
- Abundant=Temp;
- end
- end
- L=length(Abundant);
- Result=1:Input;
- for jj=1:L
- Result=setdiff(Result,Abundant+Abundant(jj));%巧妙,将2重循环变为单重循环
- end
- Output=sum(Result);
- toc
- disp('此代码使用matlab编程')
- disp(['Problem23所用时间为',num2str(toc),'秒'])
- disp(['Problem23的答案为',num2str(Output)])
- end
- %% 子程序
- %输入一个数,得到其真因子序列,顺序排列,入10,--1,2,5..
- function Output=Factor_Rank(Input)
- if nargin==0
- Input=12;
- end
- Node=floor(sqrt(Input));
- Factor=[];
- if Node*Node==Input
- Factor=Node;
- Middle=Node-1;%最中间的约数
- else
- Middle=Node;
- end
- for ii=Middle:-1:2
- if mod(Input,ii)==0
- Temp=[ii,Factor,Input/ii];
- Factor=Temp;
- end
- end
- Output=[1,Factor];
- end
复制代码 |
|