Multiples of 3 and 5

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.

Find the sum of all the multiples of 3 or 5 below 1000.


Idea

n: upper bound $$result = 3*\sum_{i=0}^{\left\lfloor \frac{n-1}{3} \right\rfloor} i + 5*\sum_{i=0}^{\left\lfloor \frac{n-1}{5} \right\rfloor} i - 15*\sum_{i=0}^{\left\lfloor \frac{n-1}{15} \right\rfloor} i$$


In [1]:
import math
In [2]:
def solve(bound):
    def partial_sum(d):
        item_num = math.floor((bound - 1) / d)
        return int(item_num * (item_num + 1) / 2)
    return 3 * partial_sum(3) + 5 * partial_sum(5) - 15 * partial_sum(15)
In [3]:
solve(10)
Out[3]:
23
In [4]:
solve(1000)
Out[4]:
233168