找不到好的方法,只好这样先完成了
看了看别人写的代码,还有不少人根据数列的规律优化了下.
规律大致是这样的
Fibonacci 数列 为(x=y=1)
x,y,x+y,x+2y,2x+3y,3x+5y...
红色的是偶数.
这样代码就可以写成下面
还有一个叫olegyk 的家伙写的
J:
fib=: {."1@(]`(({: , +/)@])@.(> {:)^:(<_))&1 2
+/ (#~ -.@(2&|)) fib 1e6
这是啥没看明白.哈哈~~
附上题目:
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, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.