# Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))

Given a positive integer n. The problem is to find the sum of the given series 1 + (1+2) + (1+2+3) + (1+2+3+4) + …… + (1+2+3+4+…+n), where i-th term in the series is the sum of first i odd natural numbers.

Examples:

```Input : n = 2
Output : 5
(1) + (1+3) = 5

Input : n = 5
Output : 55
(1) + (1+3) + (1+3+5) + (1+3+5+7) + (1+3+5+7+9) = 55
```

## Recommended: Please solve it on PRACTICE first, before moving on to the solution.

Naive Approach: Using two loops get the sum of each i-th term and then add those sum to the final sum.

## C++

 `// C++ implementation to find the  ` `// sum of the given series ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// functionn to find the  ` `// sum of the given series ` `int` `sumOfTheSeries(``int` `n) ` `{ ` `    ``int` `sum = 0; ` `    ``for` `(``int` `i = 1; i <= n; i++) { ` ` `  `        ``// first term of each i-th term ` `        ``int` `k = 1; ` `        ``for` `(``int` `j = 1; j <= i; j++) { ` `            ``sum += k; ` ` `  `            ``// next term ` `            ``k += 2; ` `        ``} ` `    ``} ` ` `  `    ``// required sum ` `    ``return` `sum; ` `} ` ` `  `// Driver program  ` `int` `main() ` `{ ` `    ``int` `n = 5; ` `    ``cout << ``"Sum = "` `         ``<< sumOfTheSeries(n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to find  ` `// the sum of the given series ` `import` `java.util.*; ` ` `  `class` `GFG { ` `     `  `    ``// functionn to find the sum ` `    ``// of the given series ` `    ``static` `int` `sumOfTheSeries(``int` `n) ` `    ``{ ` `        ``int` `sum = ``0``; ` `        ``for` `(``int` `i = ``1``; i <= n; i++) ` `        ``{ ` `      `  `            ``// first term of each  ` `            ``// i-th term ` `            ``int` `k = ``1``; ` `            ``for` `(``int` `j = ``1``; j <= i; j++) ` `            ``{ ` `                ``sum += k; ` `      `  `                ``// next term ` `                ``k += ``2``; ` `            ``} ` `        ``} ` `      `  `        ``// required sum ` `        ``return` `sum; ` `    ``} ` ` `  `    ``/* Driver program */` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `         ``int` `n = ``5``; ` `         ``System.out.println(``"Sum = "` `+  ` `                        ``sumOfTheSeries(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Arnav Kr. Mandal. `

## Python3

 `# Python3 implementation to find ` `# the sum of the given series ` ` `  `# functionn to find the sum ` `# of the given series ` `def` `sumOfTheSeries( n ): ` `    ``sum` `=` `0` `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` `         `  `        ``# first term of each i-th term ` `        ``k ``=` `1` `        ``for` `j ``in` `range``(``1``,i``+``1``): ` `            ``sum` `+``=` `k ` `             `  `            ``# next term ` `            ``k ``+``=` `2` `             `  `    ``# required sum ` `    ``return` `sum` `     `  `# Driver program ` `n ``=` `5` `print``(``"Sum ="``, sumOfTheSeries(n)) ` ` `  `# This code is contributed by "Sharad_Bhardwaj". `

## C#

 `// C# implementation to find ` `// the sum of the given series ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// functionn to find the sum ` `    ``// of the given series ` `    ``static` `int` `sumOfTheSeries(``int` `n) ` `    ``{ ` `        ``int` `sum = 0; ` `        ``for` `(``int` `i = 1; i <= n; i++) { ` ` `  `            ``// first term of each ` `            ``// i-th term ` `            ``int` `k = 1; ` `            ``for` `(``int` `j = 1; j <= i; j++) { ` `                ``sum += k; ` ` `  `                ``// next term ` `                ``k += 2; ` `            ``} ` `        ``} ` ` `  `        ``// required sum ` `        ``return` `sum; ` `    ``} ` ` `  `    ``/* Driver program */` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 5; ` `        ``Console.Write(``"Sum = "` `+  ` `                     ``sumOfTheSeries(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## php

 ` `

Output:

```Sum = 55
```

Efficient Approach:
Let an be the n-th term of the given series.

```an = (1 + 3 + 5 + 7 + (2n-1))
= sum of first n odd numbers
= n2
```

Refer this post for the proof of above formula.

Now,

Refer this post for the proof of above formula.

## C++

 `// C++ implementation to find the sum ` `// of the given series ` `#include ` `using` `namespace` `std; ` ` `  `// functionn to find the sum ` `// of the given series ` `int` `sumOfTheSeries(``int` `n) ` `{ ` `    ``// required sum ` `    ``return` `(n * (n + 1) / 2) * ` `            ``(2 * n + 1) / 3; ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``int` `n = 5; ` `    ``cout << ``"Sum = "` `         ``<< sumOfTheSeries(n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to find  ` `// the sum of the given series ` `import` `java.io.*; ` ` `  `class` `GfG { ` `     `  `// function to find the sum ` `// of the given series ` `static` `int` `sumOfTheSeries(``int` `n) ` `{ ` `    ``// required sum ` `    ``return` `(n * (n + ``1``) / ``2``) * ` `            ``(``2` `* n + ``1``) / ``3``; ` `} ` `     `  ` `  `// Driver program to test above ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `n = ``5``; ` `     `  `    ``System.out.println(``"Sum = "``+  ` `                ``sumOfTheSeries(n)); ` ` `  `} ` ` `  `} ` ` `  `// This code is contributed by Gitanjali. `

## Python3

 `# Python3 implementation to find ` `# the sum of the given series ` ` `  `# functionn to find the sum ` `# of the given series ` `def` `sumOfTheSeries( n ): ` `     `  `    ``# required sum ` `    ``return` `int``((n ``*` `(n ``+` `1``) ``/` `2``) ``*` `            ``(``2` `*` `n ``+` `1``) ``/` `3``) ` `             `  `# Driver program to test above ` `n ``=` `5` `print``(``"Sum ="``, sumOfTheSeries(n)) ` ` `  `# This code is contributed by "Sharad_Bhardwaj". `

## C#

 `// C# implementation to find ` `// the sum of the given series ` `using` `System; ` ` `  `class` `GfG { ` ` `  `    ``// function to find the sum ` `    ``// of the given series ` `    ``static` `int` `sumOfTheSeries(``int` `n) ` `    ``{ ` `        ``// required sum ` `        ``return` `(n * (n + 1) / 2) *  ` `                      ``(2 * n + 1) / 3; ` `    ``} ` ` `  `    ``// Driver program to test above ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 5; ` ` `  `        ``Console.Write(``"Sum = "` `+  ` `                   ``sumOfTheSeries(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```Sum = 55
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.

Improved By : Sam007

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.