本站遷移

因為我最近租用了網路空間以及網域,
故本站已遷移至新網站~
這邊的資訊已經正在進行搬移的工作~
希望各位可以到新網站去逛XD

New Website:
http://knightzone.org/

搜尋此網誌

2011年4月8日 星期五

[Project Euler]Problem 2 - By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

先建一個小於等於四百萬的費氏數列表,
然後把每一個值是偶數的項都加起來即可。

[Ruby]
f = Array.new
f[1] = 1
f[2] = 1
i = 2
while f[i] <= 4000000
i = i+1
f[i] = f[i-1] + f[i-2]
end
j = 1
sum = 0
while j < i
if f[j] % 2 == 0
sum += f[j]
end
j = j+1
end
p sum
view raw p2.rb hosted with ❤ by GitHub

0 意見:

張貼留言