Sum of square of first n odd numbers

13 Jun, 2022
Given a number n, find sum of square of first n odd natural numbers.
Examples : 
Input : 3
Output : 35 
12 + 32 + 52 = 35

Input : 8
Output : 680
12 + 32 + 52 + 72 + 92 + 112 + 132 + 152 
A simple solution is to traverse through n odd numbers and find the sum of square. 
Below is the implementation of the approach.  
C++
Java
Python3
C#
PHP
Javascript
// Simple C++ method to find sum of square of// first n odd numbers.#include <iostream>using namespace std; int squareSum(int n){    int sum = 0;    for (int i = 1; i <=  n; i++)        sum += (2*i - 1) * (2*i - 1);    return sum;} int main(){    cout << squareSum(8);    return 0;}
// Simple Java method to// find sum of square of// first n odd numbers. import java.io.*; class GFG {         static int squareSum(int n)    {        int sum = 0;        for (int i = 1; i <=  n; i++)            sum += (2*i - 1) * (2*i - 1);        return sum;    }          //Driver Code    public static void main(String args[])    {          System.out.println(squareSum(8));    }} // This code is contributed by// Nikita tiwari.
# Simple Python method# to find sum of square# of first n odd numbers.def squareSum(n):         sm = 0    for i in range(1, n + 1):        sm += (2 * i - 1) * (2 * i - 1)             return sm # Driver Coden=8print(squareSum(n)) # This code is contributed by Ansu Kumari
// Simple C# method to find// sum of square of first// n odd numbers.using System; class GFG {         static int squareSum(int n)    {        int sum = 0;        for (int i = 1; i <= n; i++)            sum += (2*i - 1) * (2*i - 1);        return sum;    }         // Driver Code    public static void Main()    {        Console.Write(squareSum(8));    }} // This code is contributed by// vt_m.
<?php// Simple PHP method to find sum// of square of first n odd numbers. function squareSum( $n){    $sum = 0;    for ($i = 1; $i <= $n; $i++)        $sum += (2*$i - 1) * (2*$i - 1);    return $sum;}      echo squareSum(8); // This code is contributed by Vt_m.?>
<script> // Simple Javascript method to find// sum of square of first n odd numbers.function squareSum(n){    let sum = 0;    for(let i = 1; i <=  n; i++)        sum += (2 * i - 1) * (2 * i - 1);             return sum;} // Driver codedocument.write(squareSum(8)); // This code is contributed by souravmahato348 </script>
Output : 
680
Time Complexity: O(n), where n represents the given integer.Auxiliary Space: O(1), no extra space is required, so it is a constant.An efficient solution is to apply below formula.
sum = n * (4n2 - 1) / 3

How does it work? 
Please refer sum of squares of even and odd
numbers for proof.
C++
Java
Python3
C#
PHP
Javascript
// Efficient C++ method to find sum of// square of first n odd numbers.#include <iostream>using namespace std; int squareSum(int n){    return n*(4*n*n - 1)/3;} int main(){    cout << squareSum(8);    return 0;}
// Efficient Java method// to find sum of// square of first n odd numbers. import java.io.*; class GFG {         static int squareSum(int n)    {        return n*(4*n*n - 1)/3;    }          public static void main(String args[])    {        System.out.println(squareSum(8));    }} // This code is contributed by// Nikita tiwari.
# Python3 code to find sum# of square of first n odd numbers def squareSum( n ):         return int(n * ( 4 * n * n - 1) / 3) # driver codeans = squareSum(8)print (ans) # This code is contributed by Saloni Gupta
// Efficient C# method to// find sum of square of// first n odd numbers.using System; class GFG {         static int squareSum(int n)    {        return n * (4 * n * n - 1)/3;    }         // driver code       public static void Main()    {        Console.Write(squareSum(8));    }} // This code is contributed by// Vt_m.
<?php// Efficient PHP method to find sum of// square of first n odd numbers. function squareSum($n){    return $n * (4 * $n * $n - 1) / 3;}      echo squareSum(8); // This code is contributed by Vt_m.?>
<script> // JavaScript program to find sum of// square of first n odd numbers.     function squareSum(n)    {        return n*(4*n*n - 1)/3;    } // Driver code             document.write(squareSum(8)); </script>
Output : 
680
Time Complexity: O(1), the code will run in a constant time.Auxiliary Space: O(1), no extra space is required, so it is a constant.
vt_m
souravmahato348
code_hunt
tamanna17122007
number-theory
Numbers
series
series-sum
Mathematical
number-theory
Mathematical
series
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Program for Decimal to Binary Conversion