How to find the sum of all the multiples of 3 or 5 below 1000 in Python?

后端 未结 18 1134
萌比男神i
萌比男神i 2020-12-29 12:10

Not sure if I should\'ve posted this on math.stackexchange instead, but it includes more programming so I posted it here.

The question seems really simple, but I\'ve

18条回答
  •  悲&欢浪女
    2020-12-29 12:59

    max = 1000  # notice this here
    result = 0
    for i in range(0,max):
    if i%3 == 0 or i%5 == 0:
        result += i
    
    print result
    

    This works, but use 1000 for max, so it includes 999 too.

提交回复
热议问题