There is no point in calculating fib of old numbers, you can save it instead of calculating each time.
Oh yes, I think so! How do you like this?:
const memoizer = inits => rule =>{ const memo = [...inits] const elem = n => n >= memo.length ? memo[n] = rule(elem)(n) : memo[n] return elem } const fib = memoizer( [0, 1] )( a => n => a(n-1) + a(n-2) ) const oddFibG = function*(){ let i = 0 let value while (true){ value = fib(i) if (value % 2 === 1) yield value i = i + 1 } } const sumFibs = n => { let sum = 0 for ( const i of oddFibG() ) { if ( i > n ) return sum sum = sum + i } }
It's a little more complex, but very much faster than the above.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There is no point in calculating fib of old numbers, you can save it instead of calculating each time.
Oh yes, I think so!
How do you like this?:
It's a little more complex, but very much faster than the above.