17 May 2011

Euler Project5 欧拉工程5



2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

找到可以整除1-20的最小正整数. 分析过程:

% 整除1-20,其实就是1-20的最小公倍数,计算时可以精简:
% 1-20只需要求[2,3,4,5,6,7,11,13,17,19]的最小公倍数即可
% 其余的
% 1不用计算
% 8 = 2*4
% 9 = 3*3(第二个3来自6=2*3)
% 10 = 2*5
% 12 = 3*4
% 14 = 2*7
% 16 = 2*8
% 18 = 2*9 = 2*3*3(第二个3来自6=2*3)
% 20 = 2*10 = 2*5*2(第二个2来自4=2*2)

==============
代码:
function result = euler5()
tic;
result = prod([2,3,4,5,6,7,11,13,17,19]); % => 8,9(3*3),10,12,14,15,16(2*4*2),18,20
toc;
end

运行时间及结果:
% Elapsed time is 0.000012 seconds.
% ans =
% 232792560

No comments :

Post a Comment