Skip to content
Advertisement

How to display even Fibonacci numbers by specifying their number?

I have a function that checks Fibonacci numbers for even.

JavaScript

I need it to output a specified number of even numbers

Example input: 4

Example output: 0 2 8 34

Advertisement

Answer

You could just go over the even ones directly:

JavaScript

Consider two adjacent Fibonacci numbers a and b (initially 1 and 0) and what happens when you shift the window:

JavaScript

So every third Fibonacci number is even and that last line also tells you how to shift by three steps directly.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement