Project Euler_Problem 1(MATLAB)
problem 1 Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
%定义三个变量的初始值为0
s1 = 0;
s2 = 0;
s3 = 0;
%求出小于1000的3的整数倍的和s1
for i=0:3:999
s1 = s1 + i;
end
disp(s1);%输出s1
%求出3和5的最小公倍数15在1000以内的倍数的和
for i = 0:15:999
s3 = s3 + i;
end
disp(s3);%输出s3
%求出和应该是3的倍数和与5的倍数和再减去3的倍数与5的倍数重合的数(即15的倍数和)
s = s1 + s2 - s3;
disp(s);%输出和s