codewars练习(6)Sum of all the multiples of 3 or 5

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010082526/article/details/85047901

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.

Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.

Note: If the number is a multiple of both 3 and 5, only count it once.

def solution(number):
    sum=0
    for val in range(1,number):
        if val%3==0 or val%5==0:
            sum+=val
    print(sum)

if __name__ == '__main__':    
    solution(21)

猜你喜欢

转载自blog.csdn.net/u010082526/article/details/85047901
ALL
今日推荐