Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
Given a series 12 + 32 + 52 + 72 + . . . + (2*n – 1)2, find sum of the series.
Examples:
Input : n = 4
Output : 84
Explanation :
sum = 12 + 32 + 52 + 72
= 1 + 9 + 25 + 49
= 84
Input : n = 10
Output : 1330
Explanation :
sum = 12 + 32 + 52 + 72 + 92 + 112 + 132 + 152 + 172 + 192
= 1 + 9 + 24 + 49 + . . . + 361
= 1330
C++
// Program to find sum of series // 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. #include <bits/stdc++.h> using namespace std; // Function to find sum of series. int sumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + (2 * i - 1) * (2 * i - 1); return sum; } // Driver code int main() { int n = 10; cout << sumOfSeries(n); return 0; } |
chevron_right
filter_none
Java
// Program to find sum of series // 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. import java.io.*; class GFG { // Function to find sum of series. static int sumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + (2 * i - 1) * (2 * i - 1); return sum; } // Driver code public static void main(String[] args) { int n = 10; System.out.println( sumOfSeries(n)); } } |
chevron_right
filter_none
Python3
# Python Program to find sum of series # 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. import math # Function to find sum of series. def sumOfSeries(n): sum = 0 for i in range(1,n+1): sum = sum + (2 * i - 1) * (2 * i - 1) return sum # driver code n= 10print(sumOfSeries(n)) # This code is contributed by Gitanjali. |
chevron_right
filter_none
C#
// C# Program to find sum of series // 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. using System; class GFG { // Function to find sum of series. static int sumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + (2 * i - 1) * (2 * i - 1); return sum; } // Driver code public static void Main() { int n = 10; Console.Write( sumOfSeries(n)); } } /* This code is contributed by vt_m*/ |
chevron_right
filter_none
PHP
<?php // PHP Program to find sum of series // 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. // Function to find sum of series. function sumOfSeries($n) { $sum = 0; for ($i = 1; $i <= $n; $i++) $sum = $sum + (2 * $i - 1) * (2 * $i - 1); return $sum; } // Driver code $n = 10; echo(sumOfSeries($n)); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output:
1330
Time Complexity : O(n)
Another approach : Using formula to find sum of series :
12 + 32 + 52 +
72 + . . . + (2*n - 1)2
= (n * (2 * n - 1) * (2 * n + 1)) / 3.
Please refer sum of squares of even and odd numbers for proof.
C++
// Program to find sum of series // 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. #include <bits/stdc++.h> using namespace std; // Function that find sum of series. int sumOfSeries(int n) { // Formula to find sum of series. return (n * (2 * n - 1) * (2 * n + 1)) / 3; } // Driver code int main() { int n = 10; cout << sumOfSeries(n); return 0; } |
chevron_right
filter_none
Java
// Java Program to find sum of series // 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. import java.io.*; import java.util.*; class GFG { // Function to find sum of series. static int sumOfSeries(int n) { // Formula to find sum of series. return (n * (2 * n - 1) * (2 * n + 1)) / 3; } // Driver function public static void main (String[] args) { int n=10; System.out.println(sumOfSeries(n)); } } // This code is contributed by Gitanjali. |
chevron_right
filter_none
Python3
# Python Program to find sum of series # 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. import math # Function to find sum of series. def sumOfSeries(n): # Formula to find sum of series. return int((n * (2 * n - 1) * (2 * n + 1)) / 3) # driver code n=10print(sumOfSeries(n)) # This code is contributed by Gitanjali. |
chevron_right
filter_none
C#
// C# Program to find sum of series // 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. using System; class GFG { // Function to find sum of series. static int sumOfSeries(int n) { // Formula to find sum of series. return (n * (2 * n - 1) * (2 * n + 1)) / 3; } // Driver function public static void Main () { int n = 10; Console.Write(sumOfSeries(n)); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP Program to find sum of series // 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2. // Function that find sum of series. function sumOfSeries($n) { // Formula to find sum of series. return ($n * (2 * $n - 1) * (2 * $n + 1)) / 3; } // Driver code $n = 10; echo(sumOfSeries($n)); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output:
1330
Time Complexity: O(1)
Recommended Posts:
- Sum of Series (n^2-1^2) + 2(n^2-2^2) +....n(n^2-n^2)
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Sum of series (n/1) + (n/2) + (n/3) + (n/4) +.......+ (n/n)
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ...... + (1+2+3+4+...+n)
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n
- Sum of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + …… + (2+4+6+8+….+2n)
- Sum of the series 3, 20, 63, 144, ......
- Sum of series 1*1*2! + 2*2*3! + ……..+ n*n*(n+1)!
- Sum of series 1*1! + 2*2! + ……..+ n*n!
- Sum of the series 2^0 + 2^1 + 2^2 +.....+ 2^n
- Sum of the series 1.2.3 + 2.3.4 + ... + n(n+1)(n+2)
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . .
- Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ....... (K-1)n
- Sum of the series 1^1 + 2^2 + 3^3 + ..... + n^n using recursion
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 : jit_t



