MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / EvenFibonacci

Function EvenFibonacci

Project-Euler/Problem002.js:7–18  ·  view source on GitHub ↗
(limit)

Source from the content-addressed store, hash-verified

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
7export 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}

Callers 1

Problem002.test.jsFile · 0.90

Calls 1

logMethod · 0.45

Tested by

no test coverage detected