tag:blogger.com,1999:blog-612275289405565943.post2692771185361663759..comments2023-10-01T07:05:48.338-07:00Comments on Many Moods: Solving Euler Project Problem #2 using RubyAnshuhttp://www.blogger.com/profile/18379710357924026688noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-612275289405565943.post-48921192689515209042009-10-22T13:47:07.603-07:002009-10-22T13:47:07.603-07:00Why so complicated? For a simple problem, wouldn&#...Why so complicated? For a simple problem, wouldn't this work? (Warning: ruby noob code coming!)<br /><br />seq = [1, 2]<br />even = [2]<br />while seq.last <= 4000000<br /> seq << seq[-1] + seq[-2]<br /> if seq.last % 2 == 0<br /> even << seq.last<br /> end<br />end<br />total = 0<br />even.each do |i|<br /> total = total + i<br />end<br />puts "Total: #{total}"Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-612275289405565943.post-20254707501653823142009-10-22T07:59:07.132-07:002009-10-22T07:59:07.132-07:00Thanks, Good to know about this solution and the t...Thanks, Good to know about this solution and the time comparision. I guess the other mathematical way , using Benet's formulae could be fatser.<br /><br />By the way, Congratulation for winning RPCFN #2 ! Just saw a tweet from Satish Talim.Anshuhttps://www.blogger.com/profile/18379710357924026688noreply@blogger.comtag:blogger.com,1999:blog-612275289405565943.post-85364192933228963562009-10-22T07:21:46.960-07:002009-10-22T07:21:46.960-07:00Another solution is to use a matrix to calculate F...Another solution is to use a matrix to calculate Fibonacci numbers. My friend pointed me to this and I thought it was pretty clever (I am admittedly math-retarded). The result calculates almost instantaneously though slightly slower than your solution (0.011s for the posted solution vs. 0.019s for the matrix solution).<br /><br />https://gist.github.com/a5b975b03bd549c9a0d7Charles Fedukehttps://www.blogger.com/profile/06057631290185370035noreply@blogger.com