16 May 2011

Euler Project4 欧拉工程4



如果一个数字正写和反写结果一样,就称为回文数,请找到可以表示为2个三位数乘积的最大回文数.
%% 3 digital number which remains same from both direction, like 9009
% == from small to big
% Elapsed time is 43.379476 seconds.
% ans =
% 906609
% == from big to small
% Elapsed time is 5.504064 seconds.
% ans =
% 906609
function result = euler4()
tic;
result = 0;
for i=999*999:-1:100*100 % ends with 0, jump over
if mod(i,10)==0
continue;
end
n = num2str(i);
if strcmp(n,n(end:-1:1))
flag = 0;
for j=999:-1:900 % max, so from 999 to 900, try
if i/j<=999 && i==fix(i/j)*j % i/j must be smaller than 999, and i/j must be integer flag = j; break; end end if flag~=0 result = i; break; end end end [j,result/j] toc; end

结果
% Elapsed time is 5.504064 seconds.
% ans =
% 906609

No comments :

Post a Comment