美文网首页
2018-06-02

2018-06-02

作者: Doraon | 来源:发表于2018-06-02 14:16 被阅读0次

01 :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

注意:由于能被15整除的数都能被3和5整除,所以应该分别求出能被3和5整除的数的和再减去能被15整除的和。


clear; clc

n =[3, 5];

m1 = fix(999/n(1));

m2 = fix(999/n(2));

m3 = fix(999/(n(1)*n(2)));

sum1 = 0; sum2 = 0; sum3 = 0;

for i = 1:1:m1

    sum1 = sum1 + n(1)*i;

end

for j = 1:1:m2

    sum2 = sum2 + n(2)*j;

end

for k = 1:1:m3

    sum3 = sum3 + (n(1)*n(2))*k;

end

sum = sum1 + sum2 - sum3

相关文章

网友评论

      本文标题:2018-06-02

      本文链接:https://www.haomeiwen.com/subject/ttdrsftx.html