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

后端 未结 18 1139
萌比男神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:52

    I think only the last few lines from your code are important. The or statement is the key statement in this code. Also rater than setting the max value to 999, you should set it to 1000 so that it will cover all values. Here is my code.

        ans=0
        for i in range(1,1000):
            if(i%3==0 or i%5==0):
                ans += i
        print(ans)
        input('press enter key to continue');#this line is only so that the screen stays until you press a key
    

提交回复
热议问题