`
ryan.liu
  • 浏览: 135809 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

用Python解答 ProjectEuler问题(1)

阅读更多
有个很有意思的网站 ProjectEuler.net ,提出了200多道数学问题,要求读者用计算机求解,不限制所用的计算机语言。
(2008年11月)试着用Python做了几道,挺有意思的。

  • Add all the natural numbers below one thousand that are multiples of 3 or 5.
  • Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
  • Find the largest prime factor of a composite number.
  • Find the largest palindrome made from the product of two 3-digit numbers.
  • What is the smallest number divisible by each of the numbers 1 to 20?


E001
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.

求1000以下,所有是3或5倍数的数之和。

def problem1():
    a, b, c = 3, 5, 999
    f = lambda x,lmt=c: x*(lmt/x)*(lmt/x+1)/2
    return f(a)+f(b)-f(a*b)

if __name__=='__main__':
    print str(problem1())

关键是 1000以内能被3整除的数之和是 3(1+2+3+...+333)
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics