题目2:在斐波那契数列中,找出4百万以下的项中偶数项之和。
projecteuler吧
全部回复
仅看楼主
level 10
xw_y_am 楼主
2012年04月28日 06点04分 1
level 10
xw_y_am 楼主
斐波那契数列中的每一项被定义为前两项之和。从1和2开始,斐波那契数列的前十项为:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
考虑斐波那契数列中数值不超过4百万的项,找出这些项中偶数项之和。
2012年04月28日 06点04分 2
level 3
没算,大概看了下,偶数列是
a_1=0,a_2=1
a_{n+1}=4*a_n+a_{n-1}, n>2
的数列~~
so just add```通项公式去屎吧~~
2012年04月28日 09点04分 3
level 3
JB
a_1=2,
not a_2=1`````
2012年04月28日 09点04分 4
level 3
--
[拍砖]
a_2=2
2012年04月28日 09点04分 5
level 10
xw_y_am 楼主
这个算法真心不错
2012年04月28日 12点04分 6
level 5
好吧 我还是用笨方法
def fib(lim):
^^^^a, b = 1, 1
^^^^while b < lim:
^^^^^^^^if b % 2 == 0:
^^^^^^^^^^^^yield b
^^^^^^^^a, b = b, a + b
^^^^return
def main():
^^^^ret = 0
^^^^for n in fib(4000000):
^^^^^^^^ret += n
^^^^print("result: %d" % (ret))
if __name__ == '__main__':
^^^^main()
2012年04月29日 01点04分 7
level 6
话说那个中文翻译是不是有问题啊……原文是:
find the sum of the even-valued terms
应该是指“值为偶数的项”吧~
2012年04月29日 02点04分 8
level 6
偶数项在数学里一般是指序号为偶数的项吧~~~
2012年04月29日 02点04分 9
level 10
xw_y_am 楼主
他的意思可能是偶数的项吧。。。
2012年04月29日 05点04分 10
level 10
xw_y_am 楼主
这个还好吧,也不算太慢的
2012年04月29日 05点04分 11
level 6
按“值为偶数的项”来算才能得出正确结果,如果是按“第偶数项”得不出的
2012年04月29日 05点04分 12
level 6
[吐舌]这样会有歧义吧~~~死理性派轻度强迫症表示~~~要改过来阿!!!我已经给那个网站的管理员发邮件了
2012年04月29日 05点04分 13
level 10
xw_y_am 楼主
你果然比我强迫症还厉害。。。
2012年04月29日 05点04分 14
1