`
诗意的栖居
  • 浏览: 268417 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

No.1 The sum of all the multiples of 3 or 5 below 1000

 
阅读更多
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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics