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.
A:
#! /usr/bin/env python
#method 1
find = reduce(lambda x,y:x+y,[x for x in xrange(3,1000) if not x%3 or not x%5])
print find
#method 2
result = 0
for i in range(1,1000):
if not i%3 or not i%5:
result += i
print "result=%s" %result
#method3
num = reduce(lambda x,y:x+y,[x for x in filter(lambda a: not a%3 or not a%5,range(1,1000))])
print "num=%s" %num
Find the sum of all the multiples of 3 or 5 below 1000.
A:
#! /usr/bin/env python
#method 1
find = reduce(lambda x,y:x+y,[x for x in xrange(3,1000) if not x%3 or not x%5])
print find
#method 2
result = 0
for i in range(1,1000):
if not i%3 or not i%5:
result += i
print "result=%s" %result
#method3
num = reduce(lambda x,y:x+y,[x for x in filter(lambda a: not a%3 or not a%5,range(1,1000))])
print "num=%s" %num