- Even Fibonacci Numbers (Python vs. JavaScript)
- Ethan Jarrell
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: zephyrJS
- Proofread by: Hexianga, Starriers
Even numbers in the Fibonacci sequence (Python vs. JavaScript)
Somehow generating the Fibonacci sequence is a popular interview question for employers. Finding all the even numbers in the Fibonacci sequence is a popular variation. Here, I’ll do it both in Python and JavaScript. To make things even easier, we will only generate even numbers in sequences under 4,000,000 and sum them.
What are Fibonacci numbers?
Each new term in the Fibonacci sequence is equal to the sum of the previous two terms. So, we see an example where, starting with 1 and 2, the first 10 digits in the sequence are:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89
How are we going to generate all the numbers in the sequence?
First, we can think about generating a sequence in a way like the following:
The problem here is that we can’t create a variable for every number, so a better solution would be to reassign all three variables every time we call a + b = c. So now we assign the value of the previous B to A, the value of the previous C to B, and so on. It would look something like this:
So the initial idea is that, in some loop, we check to make sure that the 4,000,000 threshold is not triggered, then we reset the values of A, B, and C, followed by storing C in an array or list. Finally, we’ll sum this array or list.
So much for pseudo-code, let’s show some sample code to see what it might look like:
Python:
Let’s start as pseudocode. I assign the empty array to the variable ‘x’.
x = []
a = 1
b = 2
c = a + b
Copy the code
Next, I’ll use Python’s while loop to check and make sure the value of C is less than 4000000.
while c < 4000000:
a = b
b = c
c = a + b
if c % 2= =0:
x.insert(0, c)
Copy the code
Since we only need even numbers, inside the while we’ll check to make sure it’s an even number before we do an insert to x. In the operation. Next, we’ll sum the numbers in the list and print the value in Python.
numSum = (sum(x))
print numSum
Copy the code
JavaScript:
I’d like to do it in JavaScript, but it’s a little different than Python. First I’ll create an empty array and then assign the first two indexes to the array:
var fib = [];
fib[0] = 1;
fib[1] = 2;
Copy the code
Next, I’ll loop through the array. Select the index I need to generate the Fibonacci sequence. In the previous example, we reset the values of A, B, and C each time through the loop. But in this version, we will not reset any of the values. Instead, I will assign the values of f[I] + f[I -1] to f[I] and store the values of f[I] into an array.
for(i=2; i<=50; i++) {
fib[i] = fib[i2 -] + fib[i- 1];
fib.push(fib[i]);
}
Copy the code
At this point, I have a complete Fibonacci sequence, but not just a sequence of even numbers, so I’ll use the second loop to get an array of less than 4,000,000 even numbers.
arrUnder4mil = [];
for (var i = 0; i < fib.length; i++) {
if (fib[i] <= 4000000 && fib[i] %2= =0) { arrUnder4mil.push(fib[i]); }}Copy the code
Finally, I’ll sum the numbers in the array and print the result.
let fibSum = arrUnder4mil.reduce((a, b) = > a + b, 0);
console.log(fibSum);
Copy the code
Conclusion:
Although our JavaScript code is a bit heavy, both approaches solve the problem in milliseconds. In my opinion, for these technical interviews, using two different approaches or languages can help the employer discover your versatility and creativity. But most importantly, it shows your ability to think logically. If you have any feedback, please contact me. Thank you very much!
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.