Sum of Fibonacci Numbers | C # Program to find sum of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci  number using table f[] ; Arrays . fill ( f , 0 ) ; 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 first Fibonacci numbers ; Driver Code
using System ; class GFG { static int MAX = 1000 ; static int [ ] f = new int [ MAX ] ; static int fib ( int n ) { for ( int i = 0 ; i < MAX ; i ++ ) f [ i ] = 0 ; if ( n == 0 ) return 0 ; if ( n == 1 n == 2 ) return ( f [ n ] = 1 ) ; if ( f [ n ] == 1 ) return f [ n ] ; int k ; if ( ( n & 1 ) == 1 ) k = ( n + 1 ) / 2 ; else k = n / 2 ; if ( ( n & 1 ) == 1 ) f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) ; else f [ n ] = ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } static int calculateSum ( int n ) { return fib ( n + 2 ) - 1 ; } public static void Main ( ) { int n = 4 ; Console . Write ( " Sum ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " + calculateSum ( n ) ) ; } }