Python练习题 029:Project Euler 001:3和5的倍数

时间:2021-07-17 11:47:49

开始做 Project Euler 的练习题。网站上总共有565题,真是个大题库啊!

# Project Euler, 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.
# Answer:233168

sum = 0
for i in range(1,1000):
    if i%3 == 0 or i%5 == 0:
        sum += i
print(sum)

求1000以内所有3和5的倍数的总和。用求余数的方法简单判断一下1-999之间的所有数字,只要能被3或5整除,就把它加进 sum 里。倒也不难。