400万内斐波那契数的偶数之和(Project Euler Problem 2)
2016-10-07 17:04
411 查看
题目原文:
意思是:求400万内斐波那契数的偶数之和
我的一个解法:
1 不够严谨
2 效率较低
def fibonacci(n):
"""递归函数
输出斐波那契数列"""
if n <= 1:
return n
else:
return (fibonacci(n - 1) + fibonacci(n - 2))
index =0
count =0
fibNum =1
while fibNum < 4000000:
index+=1
fibNum = fibonacci(index) #当前索引的斐波那契
if fibNum %2 ==0:
count += fibNum
print(count)
一个更加高效的解法:
limit = 4000000
sum = 0
a = 1
b = 1
while b < limit:
if b % 2 == 0:
sum += b
h = a + b
a = b #前一个斐波那契额数
b = h #当前斐波那契数
print(sum)
源码和官方解答:https://yunpan.cn/cvwfYpphfgz3a
访问密码 bfc7
意思是:求400万内斐波那契数的偶数之和
我的一个解法:
1 不够严谨
2 效率较低
def fibonacci(n):
"""递归函数
输出斐波那契数列"""
if n <= 1:
return n
else:
return (fibonacci(n - 1) + fibonacci(n - 2))
index =0
count =0
fibNum =1
while fibNum < 4000000:
index+=1
fibNum = fibonacci(index) #当前索引的斐波那契
if fibNum %2 ==0:
count += fibNum
print(count)
一个更加高效的解法:
limit = 4000000
sum = 0
a = 1
b = 1
while b < limit:
if b % 2 == 0:
sum += b
h = a + b
a = b #前一个斐波那契额数
b = h #当前斐波那契数
print(sum)
源码和官方解答:https://yunpan.cn/cvwfYpphfgz3a
访问密码 bfc7
相关文章推荐
- 斐波那契数列偶数项求和(Project Euler Problem 2)
- http://projecteuler.net/problem=17 [Answer:21124]
- http://projecteuler.net/problem=39 [Answer:840]
- project euler Problem 56
- Project Euler Problem 9
- Project Euler Problem 76-整数分拆
- Largest product in a grid( Project Euler problem 11)
- Project Euler -> problem 6
- project euler problem 10:Summation of primes
- project euler problem 11
- project euler problem 12
- Project Euler problem 9 勾股数
- Project Euler problem 30
- Project Euler problem 41
- 欧拉,projecteuler Problem 83
- Project Euler Problem 11: Largest product in a grid
- Project Euler Problem 67 Maximum path sum II
- projecteuler Problem 20
- project euler Problem 39
- http://projecteuler.net/problem=18 [Answer:1074]