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

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

    I know it was 7 years ago but I wanna share my solution to this problem.

    x= set()
    for i in range(1,1001):
        if (i % 3) == 0:
            x.add(i)
    
    for j in range(1,1001):
        if (j % 5) == 0:
            x.add(j)
    
    print(sum(x))
    

提交回复
热议问题