I had a blast reading through the many creative and varied approaches to solving Problem #1. There's something very fun and enlightening about seeing the same problem solved in different ways:
Not sure if this will become a regular thing, but I wanted to keep this rolling and present Problem #2. Again, this is from the wonderful Project Euler.
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, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Look forward to seeing solutions in your language of choice!
Latest comments (26)
Here was my solution in Javascript to the problem on Euler
function fiboEvenSum(n) { let previous = 0, current = 1, result = 0; for(let i = 0;i const next = previous + current; if(next % 2 ===0){ result += next; } if(next>n)break; previous = current; current = next; } return result; } fiboEvenSum(10);It's cool to see all the different solutions here. I wrote a post that covers the Fibonacci sequence last month. 😃
Clojure 5-10ms
I have just given it a try in Ruby
A simple iterative Rust Solution.
Takes about 0.9 sec, and optimized one takes 0.35 sec
SQL
I have an answer, but I'm not 100% happy with it. It's not dynamic enough, but I need to do some more digging into Cycles it seems :)
Uses recursion, can someone explain how to calculate the complexity?
My original solution (Ruby)
I also thought it would be fun to try it as a lazy enumerator, though I still like my original solution better.
In Javascript using filter and reduce.
Haskell:
Some comments may only be visible to logged-in visitors. Sign in to view all comments.