扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:博客网 来源:博客网 2007年8月17日
关键字:
在本页阅读全文(共19页)
原题:
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.
测试代码:
from time import clock as now
def getNatureSum(beg , max):
n = max / beg
end = beg * n
return (beg + end) * n / 2
def getSum(max) :
print getNatureSum( 3 , max ) + getNatureSum( 5 , max) - getNatureSum(15 , max)
start = now()
getSum(999)
finish = now()
print (finish-start)
改进后的代码:
from time import clock as now
start = now()
a = range(3 , 1000 , 3)
b = range(5 , 1000 , 5)
c = range(15 , 1000 , 15)
print sum(a) + sum(b) - sum(c)
finish = now()
print (finish-start)
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。