projecteuler(欧拉几何)官网 https://projecteuler.net/archives
练习自己的code能力,完成projecteuler的题目,做一个归档记录。
第1题:
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的数,并计算出它们的和。
#!/usr/bin/env python # -*- coding: utf-8 -*- def Multiples_of_3_and_5(num): sum=0 for n in xrange(num): if not n%3 or not n%5: sum+=n return sum if __name__ == "__main__": print Multiples_of_3_and_5(1000)
第2题:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
一个斐波那契数列,以1和2开始。求出不大于四百万的偶数的和。
#!/usr/bin/env python # -*- coding: utf-8 -*- def fib(): x,y=1,2 sum=2 #这里是偶数的和,所以初始sum为2 while sum<4000000: x,y=y,x+y if not y%2: sum+=y return sum if __name__ == "__main__": print fib()