Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2

15 Jun, 2022
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++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    int n = 10;     cout << sumOfSeries(n);     return 0;}
// 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));   }     }
# 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 coden= 10print(sumOfSeries(n)) # This code is contributed by Gitanjali.
// 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 codepublic static void Main(){    int n = 10;    Console.Write( sumOfSeries(n));}} /* This code is contributed by vt_m*/
<?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.?>
<script> // JavaScript 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){    let sum = 0;    for(let i = 1; i <= n; i++)       sum = sum + (2 * i - 1) *                   (2 * i - 1);            return sum;} // Driver Codelet n = 10; document.write(sumOfSeries(n));    // This code is contributed by avijitmondal1998 </script>
1330
 
Time Complexity : O(n)
Auxiliary Space: O(1)
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++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    int n = 10;    cout << sumOfSeries(n);    return 0;}
// 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.
# 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 coden=10print(sumOfSeries(n)) # This code is contributed by Gitanjali.
// 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 functionpublic static void Main (){    int n = 10;    Console.Write(sumOfSeries(n));}} // This code is contributed by vt_m.
<?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.?>
<script>// Javascript 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 codelet n = 10;document.write(sumOfSeries(n)); // This code is contributed by _saurabh_jaiswal. </script>
1330
 
Time Complexity: O(1)
Auxiliary space: O(1) since using constant variables 
jit_t
avijitmondal1998
_saurabh_jaiswal
polymatir3j
series
series-sum
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.