Sum of Fibonacci numbers at even indexes upto N terms | javascript Program to find even indexed Fibonacci Sum in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number  using table f ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Computes value of even - indexed Fibonacci Sum ; Get n ; Find the alternating sum
var MAX = 1000 ; var f = Array ( MAX ) . fill ( 0 ) ; function fib ( n ) { if ( n == 0 ) { return 0 ; } if ( n == 1 n == 2 ) { return ( f [ n ] = 1 ) ; } if ( f [ n ] == 1 ) { return f [ n ] ; } var k = ( n % 2 == 1 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( n % 2 == 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } function calculateEvenSum ( n ) { return ( fib ( 2 * n + 1 ) - 1 ) ; } var n = 8 ; document . write ( " " + n + " " + calculateEvenSum ( n ) ) ;