Fork me on GitHub

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

Problem Statement

The various multiples of either 3 and / or 5 between 1 and 20 are 3, 5, 6, 9, 10, 12, 15 and 18. The sum of all these values is 78. Similarly we are required to find the sum of all the multiples of 3 and / or 5 between 1 and 1000.

Notes

We use a simple list comprehension using a for loop with a if condition as filter and add up all the elements in the sequence using the sum function.

Solution

print sum((
    i for i in xrange(1,1000)  
        if i % 3 == 0 or i % 5 == 0))

Comments !

social