题目描述
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.
求1000以内所有3或5倍数数字之和
分析
求1000以内所有3或5倍数数字之和。也就是所有对3或5取模为0的所有数字之和。
解题代码
def check(num):
return (num % 3 == 0) or (num % 5 == 0)
if __name__ == "__main__":
_sum = 0
for i in range(3, 1000):
_sum += i if check(i) else 0
print(_sum)