나는 이렇게 학습한다/Algorithm & SQL

Sum of all the multiples of 3 or 5

daco2020 2022. 7. 20. 23:30

Your task is to write function findSum.

Upto and including n, this function will return the sum of all multiples of 3 and 5.

For example:

findSum(5) should return 8 (3 + 5)

findSum(10) should return 33 (3 + 5 + 6 + 9 + 10)

 

 

Solution:

def find(n):
    return sum([i for i in range(1, n+1) if i%3 == 0 or i%5 == 0])

1. Find a number less than 'n'.
2. Find a number that is divisible by 3 or divisible by 5.
3. Add up all the numbers found.