# Find the sum of first N odd Fibonacci numbers

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 = 3
Output : 5
Expanation : 1 + 1 + 3

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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; ` `    ``Sum = 1; ` `    ``Sum = 2; ` `    ``Sum = 5; ` `    ``Sum = 10; ` `    ``Sum = 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;  ` `    ``Sum = 1;  ` `    ``Sum = 2;  ` `    ``Sum = 5;  ` `    ``Sum = 10;  ` `    ``Sum = 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      ` `}  `

## PHP

 ` `

Output:

```44
```

Reference: https://oeis.org/A174542 My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.