然後把每一個值是偶數的項都加起來即可。
[Ruby]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
0 意見:
張貼留言