/code/blog

Code, code and code

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
1
2
3
print sum((
    i for i in xrange(1,1000)
        if i % 3 == 0 or i % 5 == 0))

Comments