不要追求绝对的公平,红尘之中没有公平而言,人活一世,难得糊涂。                                           it is no use doing what you like, you have got to like what you do.

Problem 2

上一篇 / 下一篇  2009-09-08 10:23:21 / 个人分类:projecteuler题目 ruby实现

每日随笔
  • 每日随笔:
#   Problem 251Testing软件测试网d&u |j#eb0qQ#|
#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:
B1yR7cm1k euc+c/gi0#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...51Testing软件测试网|D#S O;T&Z%fB/P
#Find the sum of all the even-valued terms in the sequence which do not exceed four million.
7O2@0k|,m[0
t&h7q f0R'Q0=begin51Testing软件测试网7e[f,Clt
max    =4000000
#jR&dQ,X;}H/a0total  = 0
-Y1[(Hz/Qcq&w_:Jt0$stack = []
T.v)ZT_#xSlXM(x0 51Testing软件测试网 o)n+E4GW6L Z G&{G K
def fib n
"`v1j4WG5_ro0  if n == 051Testing软件测试网D1N eKU { jn8m
    return 1
hJq}_0  end51Testing软件测试网#}6A&_(b6czD%B']
  if n == 1
s CkD4E1c Hz0    return 251Testing软件测试网;xs9j/C$i(r"w
  end
y;F+`+xX0  return $stack[n - 1] + $stack[n - 2]
H]!F%HYf0end51Testing软件测试网-oDw!i"T7r!xsuq
 
*D;|&o T1~dQ~0max.times do |i|
)LG!N:C \(|0  $stack[i] = fib i51Testing软件测试网 Al'Es%`o;C*|
  if $stack[i] > max
,]%I!|7j,Xiy*lbTh0    break
5{5lP/P q0  end
`k{5tIX _&{Xxd0  if $stack[i] & 1 == 0
E h%{.dh]d3UMe]{_0    total += $stack[i]
d {L3S @K!N0  end
GwG,gp0end
!FCWZL0puts total 51Testing软件测试网*C$u1|1K/Z t1C M
=end
-S"?:ii~$o N051Testing软件测试网;\,m hC{@ fY

TAG:

 

评分:0

我来说两句

Open Toolbar