sumFibs

Medium · Mathematical Calculations · 2 test cases

Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.

Examples

sumFibs(10) → 10
sumFibs(4000000) → 4613732

Starter code

function sumFibs(num) {

}

Complexity of the optimal solution

Time O(log n), space O(1). Fibonacci numbers grow exponentially. The number of iterations needed to reach `num` (n) is proportional to log(n). Therefore, the time complexity is logarithmic. The space used is constant.

Solve sumFibs in the browser