# Find the sum of first N odd Fibonacci numbers

Last Updated : 02 Nov, 2023

Given a number, N. Find the sum of first N odd Fibonacci numbers.

Note: The answer can be very large so print the answer modulo 10^9+7.

Examples:

`Input : N = 3Output : 5Explanation : 1 + 1 + 3Input : 6Output : 44Explanation : 1 + 1 + 3 + 5 + 13 + 21`

Approach: Odd Fibonacci series is:

`1, 1, 3, 5, 13, 21, 55, 89......`

Prefix sum of odd Fibonacci series is:

`1, 2, 5, 10, 23, 44, 99, 188.....`

The formula for the sum of first N odd Fibonacci numbers is:

`a(n) = a(n-1) + 4*a(n-2) - 4*a(n-3) + a(n-4) - a(n-5) for n>5  `

Below is the implementation of the above approach:

## C++

 `// CPP program to Find the sum of` `// first N odd Fibonacci numbers` `#include ` `using` `namespace` `std;`   `#define mod 1000000007`   `// Function to calculate sum of first` `// N odd Fibonacci numbers` `long` `long` `sumOddFibonacci(``int` `n)` `{` `    ``long` `long` `Sum[n + 1];`   `    ``// base values` `    ``Sum[0] = 0;` `    ``Sum[1] = 1;` `    ``Sum[2] = 2;` `    ``Sum[3] = 5;` `    ``Sum[4] = 10;` `    ``Sum[5] = 23;`   `    ``for` `(``int` `i = 6; i <= n; i++) {` `        ``Sum[i] = ((Sum[i - 1] + (4 * Sum[i - 2]) % mod - ` `                  ``(4 * Sum[i - 3]) % mod + mod) % mod + ` `                  ``(Sum[i - 4] - Sum[i - 5] + mod) % mod) % mod;` `    ``}`   `    ``return` `Sum[n];` `}`   `// Driver code` `int` `main()` `{` `    ``long` `long` `n = 6;`   `    ``cout << sumOddFibonacci(n);`   `    ``return` `0;` `}`

## Java

 `// Java  program to Find the sum of ` `// first N odd Fibonacci numbers `   `import` `java.io.*;`   `class` `GFG {` `    ``static` `int` `mod =``1000000007``; `   `// Function to calculate sum of first ` `// N odd Fibonacci numbers ` `static`  `int` `sumOddFibonacci(``int` `n) ` `{ ` `     ``int` `Sum[]=``new` `int``[n + ``1``]; `   `    ``// base values ` `    ``Sum[``0``] = ``0``; ` `    ``Sum[``1``] = ``1``; ` `    ``Sum[``2``] = ``2``; ` `    ``Sum[``3``] = ``5``; ` `    ``Sum[``4``] = ``10``; ` `    ``Sum[``5``] = ``23``; `   `    ``for` `(``int` `i = ``6``; i <= n; i++) { ` `        ``Sum[i] = ((Sum[i - ``1``] + (``4` `* Sum[i - ``2``]) % mod - ` `                ``(``4` `* Sum[i - ``3``]) % mod + mod) % mod + ` `                ``(Sum[i - ``4``] - Sum[i - ``5``] + mod) % mod) % mod; ` `    ``} `   `    ``return` `Sum[n]; ` `} `   `// Driver code ` `    `  `    ``public` `static` `void` `main (String[] args) {`   `    ``int` `n = ``6``; ` `    ``System.out.println(sumOddFibonacci(n));` `    ``}` `//This Code is Contributed by Sachin    ` `}`

## Python3

 `# Python3 program to Find the sum of ` `# first N odd Fibonacci numbers ` `mod ``=` `1000000007` `;`   `# Function to calculate sum of ` `# first N odd Fibonacci numbers ` `def` `sumOddFibonacci(n): `   `    ``Sum``=``[``0``]``*``(n ``+` `1``); `   `    ``# base values ` `    ``Sum``[``0``] ``=` `0``; ` `    ``Sum``[``1``] ``=` `1``; ` `    ``Sum``[``2``] ``=` `2``; ` `    ``Sum``[``3``] ``=` `5``; ` `    ``Sum``[``4``] ``=` `10``; ` `    ``Sum``[``5``] ``=` `23``; `   `    ``for` `i ``in` `range``(``6``,n``+``1``): ` `        ``Sum``[i] ``=` `((``Sum``[i ``-` `1``] ``+` `                    ``(``4` `*` `Sum``[i ``-` `2``]) ``%` `mod ``-` `                    ``(``4` `*` `Sum``[i ``-` `3``]) ``%` `mod ``+` `                    ``mod) ``%` `mod ``+` `(``Sum``[i ``-` `4``] ``-` `                    ``Sum``[i ``-` `5``] ``+` `mod) ``%` `mod) ``%` `mod; `   `    ``return` `Sum``[n]; `   `# Driver code ` `n ``=` `6``; ` `print``(sumOddFibonacci(n)); `   `# This code is contributed by mits`

## C#

 `// C#  program to Find the sum of ` `// first N odd Fibonacci numbers `   `using` `System;`   `public` `class` `GFG{`   `static` `int` `mod =1000000007; ` `// Function to calculate sum of first ` `// N odd Fibonacci numbers ` `static` `int` `sumOddFibonacci(``int` `n) ` `{ ` `    ``int` `[]Sum=``new` `int``[n + 1]; `   `    ``// base values ` `    ``Sum[0] = 0; ` `    ``Sum[1] = 1; ` `    ``Sum[2] = 2; ` `    ``Sum[3] = 5; ` `    ``Sum[4] = 10; ` `    ``Sum[5] = 23; `   `    ``for` `(``int` `i = 6; i <= n; i++) { ` `        ``Sum[i] = ((Sum[i - 1] + (4 * Sum[i - 2]) % mod - ` `                ``(4 * Sum[i - 3]) % mod + mod) % mod + ` `                ``(Sum[i - 4] - Sum[i - 5] + mod) % mod) % mod; ` `    ``} `   `    ``return` `Sum[n]; ` `} `   `// Driver code ` `    `  `    `  `    `  `    ``static` `public` `void` `Main (){` `        ``int` `n = 6; ` `    ``Console.WriteLine(sumOddFibonacci(n)); ` `    ``} ` `//This Code is Contributed by Sachin     ` `} `

## Javascript

 ``

## PHP

 ``

Output

```44

```

Time Complexity: O(n), Auxiliary Space: O(n)

Efficient approach : Space optimization O(1)

In previous approach we the current value Sum[i] is only depend upon the previous 6 values i.e. Sum[i-1], Sum[i-2], …. Sum[i-5]  So to optimize the space we can keep track of previous and current values by the help of three variables Sum0, Sum1 , …. ,  Sum5 which will reduce the space complexity from O(N) to O(1).

Implementation Steps:

• Create variables Sum0, Sum1 , …. ,  Sum5 to keep track of previous values of Sum[].
• Initialize base case.
• Create a variable Sum to store current value.
• Iterate over subproblem using loop and update Sum.
• After every iteration update Sum0, Sum1 , …. ,  Sum5 for further iterations.
• At last return Sum.

Implementation:

## C++

 `// CPP program to Find the sum of` `// first N odd Fibonacci numbers` `#include ` `using` `namespace` `std;`   `#define mod 1000000007`   `// Function to calculate sum of first` `// N odd Fibonacci numbers` `long` `long` `sumOddFibonacci(``int` `n)` `{` `//     long long Sum[n + 1];`   `    ``// base values` `    ``int` `Sum0 = 0;` `    ``int` `Sum1 = 1;` `    ``int` `Sum2 = 2;` `    ``int` `Sum3 = 5;` `    ``int` `Sum4 = 10;` `    ``int` `Sum5 = 23;` `    `  `    ``int` `Sum;`   `    ``for` `(``int` `i = 6; i <= n; i++) {` `        ``Sum = ((Sum5 + (4 * Sum4) % mod -` `                ``(4 * Sum3) % mod + mod) % mod +` `                ``(Sum2 - Sum1 + mod) % mod) % mod;` `      `  `            ``//assigning values for further iteration` `            ``Sum0 = Sum1;` `            ``Sum1 = Sum2;` `            ``Sum2 = Sum3;` `            ``Sum3 = Sum4;` `            ``Sum4 = Sum5;` `            ``Sum5 = Sum;` `    ``}`   `      ``// return final answer` `    ``return` `Sum;` `}`   `// Driver code` `int` `main()` `{` `    ``long` `long` `n = 6;` `    `  `      ``// function call` `    ``cout << sumOddFibonacci(n);`   `    ``return` `0;` `}`

## Java

 `import` `java.util.*;`   `public` `class` `Main {` `  `  `    ``// Function to calculate sum of first` `    ``// N odd Fibonacci numbers` `    ``static` `long` `sumOddFibonacci(``int` `n)` `    ``{` `        ``int` `mod = ``1000000007``;`   `        ``// base values` `        ``int` `Sum0 = ``0``;` `        ``int` `Sum1 = ``1``;` `        ``int` `Sum2 = ``2``;` `        ``int` `Sum3 = ``5``;` `        ``int` `Sum4 = ``10``;` `        ``int` `Sum5 = ``23``;`   `        ``int` `Sum;`   `        ``for` `(``int` `i = ``6``; i <= n; i++) {` `            ``Sum = ((Sum5 + (``4` `* Sum4) % mod` `                    ``- (``4` `* Sum3) % mod + mod)` `                       ``% mod` `                   ``+ (Sum2 - Sum1 + mod) % mod)` `                  ``% mod;`   `            ``// assigning values for further iteration` `            ``Sum0 = Sum1;` `            ``Sum1 = Sum2;` `            ``Sum2 = Sum3;` `            ``Sum3 = Sum4;` `            ``Sum4 = Sum5;` `            ``Sum5 = Sum;` `        ``}`   `        ``// return final answer` `        ``return` `Sum5;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `N = ``6``;` `        ``System.out.println(sumOddFibonacci(N));` `    ``}` `}`

## Python

 `MOD ``=` `1000000007`   `# Function to calculate sum of first` `# N odd Fibonacci numbers` `def` `sumOddFibonacci(n):` `    ``# base values` `    ``Sum0 ``=` `0` `    ``Sum1 ``=` `1` `    ``Sum2 ``=` `2` `    ``Sum3 ``=` `5` `    ``Sum4 ``=` `10` `    ``Sum5 ``=` `23`   `    ``for` `i ``in` `range``(``6``, n``+``1``):` `        ``Sum` `=` `((Sum5 ``+` `(``4` `*` `Sum4) ``%` `MOD ``-` `                ``(``4` `*` `Sum3) ``%` `MOD ``+` `MOD) ``%` `MOD ``+` `                ``(Sum2 ``-` `Sum1 ``+` `MOD) ``%` `MOD) ``%` `MOD`   `        ``# assigning values for further iteration` `        ``Sum0 ``=` `Sum1` `        ``Sum1 ``=` `Sum2` `        ``Sum2 ``=` `Sum3` `        ``Sum3 ``=` `Sum4` `        ``Sum4 ``=` `Sum5` `        ``Sum5 ``=` `Sum`   `    ``# return final answer` `    ``return` `Sum`   `# Driver code` `n ``=` `6`   `# function call` `print``(sumOddFibonacci(n))`

## C#

 `using` `System;`   `class` `GFG` `{` `    ``const` `int` `mod = 1000000007;`   `    ``// Function to calculate the sum of the first N odd Fibonacci numbers` `    ``static` `long` `SumOddFibonacci(``int` `n)` `    ``{` `        ``long` `sum1 = 1;` `        ``long` `sum2 = 2;` `        ``long` `sum3 = 5;` `        ``long` `sum4 = 10;` `        ``long` `sum5 = 23;` `        ``long` `sum = 0;`   `        ``for` `(``int` `i = 6; i <= n; i++)` `        ``{` `            ``sum = ((sum5 + (4 * sum4) % mod - (4 * sum3) % mod + mod) % mod` `                ``+ (sum2 - sum1 + mod) % mod) % mod;`   `            ``// Shift values for further iterations` `            ``sum1 = sum2;` `            ``sum2 = sum3;` `            ``sum3 = sum4;` `            ``sum4 = sum5;` `            ``sum5 = sum;` `        ``}`   `        ``// Return the final answer` `        ``return` `sum;` `    ``}`   `    ``// Driver code` `    ``static` `void` `Main()` `    ``{` `        ``long` `n = 6;`   `        ``// Function call` `        ``long` `result = SumOddFibonacci((``int``)n);` `        ``Console.WriteLine(result);` `    ``}` `}`

## Javascript

 `function` `sumOddFibonacci(n) {` `    ``const mod = 1000000007;`   `    ``// Initialize base values` `    ``let Sum0 = 0;` `    ``let Sum1 = 1;` `    ``let Sum2 = 2;` `    ``let Sum3 = 5;` `    ``let Sum4 = 10;` `    ``let Sum5 = 23;`   `    ``let Sum;`   `    ``for` `(let i = 6; i <= n; i++) {` `        ``Sum = (((Sum5 + (4 * Sum4) % mod - (4 * Sum3) % mod + mod) % mod + (Sum2 - Sum1 + mod) % mod) % mod);`   `        ``// Assign values for further iteration` `        ``Sum0 = Sum1;` `        ``Sum1 = Sum2;` `        ``Sum2 = Sum3;` `        ``Sum3 = Sum4;` `        ``Sum4 = Sum5;` `        ``Sum5 = Sum;` `    ``}`   `    ``// Return the final answer` `    ``return` `Sum5;` `}`   `// Driver Code` `const N = 6;` `console.log(sumOddFibonacci(N));`

Output:

`44`

Time Complexity: O(n)

Auxiliary Space: O(1)

Previous
Next