| 5 | // theoretically it should take O(1) constant amount of time as long |
| 6 | // arithmetic calculations are considered to be in constant amount of time |
| 7 | export const EvenFibonacci = (limit) => { |
| 8 | if (limit < 1) |
| 9 | throw new Error("Fibonacci sequence limit can't be less than 1") |
| 10 | |
| 11 | const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)) |
| 12 | const n = Math.floor(highestIndex / 3) |
| 13 | return Math.floor( |
| 14 | ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - |
| 15 | ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / |
| 16 | SQ5 |
| 17 | ) |
| 18 | } |