About: L.A. based web developer slowly parsing through Stack Overflow. If you like hot web dev tips or stories about being a freelancer, check out my newsletter: https://codenutt.substack.com/p/coming-soon
Location:
Los Angeles
Joined:
Sep 20, 2019
Project Euler Problem 2 Solved with Javascript
Publish Date: Jan 27 '20
10 0
Welcome to Round Two of Project Euler! This time we're heading into the land of state management and that guy you've probably heard about: Fibonacci! I'm excited, are you excited?
Video Version
If you like to watch rather than read, check out the video that accompanies this article. If not, keep reading!
Problem 2: Even Fibonacci Numbers
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, ...
Problem Statement
By considering the terms in the Fibonacci sequence that do not exceed the nth term, find the sum of the even-valued terms.
Solution
Breakdown of the Problem
Fibonacci sequence
It's stated in the problem, however, let's discuss what a Fibonacci sequence is a little bit and why it's important.
The next number is found by adding up the two numbers before it.
When we do this, it creates an interesting spiral, which is found in myriad places in nature.
We could probably talk all day about Fibonacci, but let's get back to code.
Steps
Generate Fibonacci sequence
Add our first term(1) + our second term (2)
Add product of our previous numbers to second term
Check to see if the value is even, if so, add it to a total sum.
Repeat, infinitely...
Naive Approach
functionfiboEvenSum(n){// setup placeholders for our three valuesletfibNumSum=0;// product of our two numbersletfibCurrent=0;// current numberletfibNext=1;// next numberfor(leti=1;i<=n;i++){// find next numberletfibTotal=fibCurrent+fibNext;// set first number to second numberfibCurrent=fibNext;// set second number to totalfibNext=fibTotal;// make sure the value is evenif(fibTotal%2==0){fibNumSum+=fibTotal;}}// You can do it!returnfibNumSum;}fiboEvenSum(10);//44
Modulo
I talked about what modulo is in my first article, but I need to clarify a couple things. When you evaluate a modulo expression, it gives you the remainder of the division of the two numbers. In the case of 15 % 4 , that will give us a remainder of 3 because:
4 goes into 15 3 times.
15 - 3 * 4 (12) = 3
Therefore, 15 % 4 = 3 or 15 / 4 = 3 remainder 3
I don't know if that explains it well enough...but, if it still doesn't make sense. Check out the video in the resources section below!
Algorithmic Approach
I'm not going to sit here and try and explain someone else's code. However, I found a pretty awesome way of solving this problem via CodeFay.
It's an elegant approach, as well as a faster one. When I tested it against my naive approach, it was generally about 20% faster. I suggest checking it out if you want to see some cool code!
Final Thoughts
This problem is fairly straight-forward. What I like about this problem is it gets us thinking about the "state" of our app. We define our three values at the top of our function, then manipulate them within the for loop. This reduces what we have to do after we get all the fibonacci values.
As always, I'm sure there is room for improvement. If you have recommendations, let me know!