Sum of square of first n even numbers - GeeksforGeeks

08 Apr, 2021
Given a number n, find sum of square of first n even natural numbers. 
Examples : 
Input : 3
Output : 56 
22 + 42 + 62 = 56

Input : 8
Output : 816
22 + 42 + 62 + 82 + 102 + 122 + 142 + 162 
A simple solution is to traverse through n even numbers and find the sum of square.  
C++
Java
Python3
C#
PHP
Javascript
// Simple C++ method to find sum// of square of first n even numbers.#include <iostream>using namespace std; int squareSum(int n){    int sum = 0;    for (int i = 1; i <= n; i++)        sum += (2 * i) * (2 * i);    return sum;} // Driver Codeint main(){    cout << squareSum(8);    return 0;}
// Simple Java method to find sum of// square of first n even numbers.import java.io.*; class GFG{    static int squareSum(int n)    {        int sum = 0;        for (int i = 1; i <= n; i++)            sum += (2 * i) * (2 * i);        return sum;    }         // Driver Code    public static void main(String args[])                  throws IOException    {        System.out.println(squareSum(8));    }} // This code is contributed by Nikita Tiwari
# Simple Python3 code to# find sum of square of# first n even numbers def squareSum( n ):    sum = 0    for i in range (0, n + 1):        sum += (2 * i) * (2 * i)             return sum # driver codeans = squareSum(8)print (ans) # This code is contributed by Saloni Gupta
// Simple C# method to find sum of// square of first n even numbers.using System; class GFG{    static int squareSum(int n)    {        int sum = 0;        for (int i = 1; i <= n; i++)            sum += (2 * i) * (2 * i);                 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 even numbers. function squareSum($n){    $sum = 0;    for ($i = 1; $i <= $n; $i++)        $sum += (2 * $i) * (2 * $i);    return $sum;}      echo squareSum(8);  // This code is contributed by vt_m.?>
<script> // Simple Javascript method to find sum// of square of first n even numbers.function squareSum(n){    sum = 0;    for(let i = 1; i <= n; i++)        sum += (2 * i) * (2 * i);             return sum;} // Driver Codedocument.write(squareSum(8)); // This code is contributed by souravmahato348 </script>
Output: 
816
An efficient solution is to apply below formula.
sum = 2 * n * (n + 1) * (2 * n + 1)/3

How does it work? 
We know that sum of square of first 
n natural numbers is = n(n+1)/2

Sum of square of first n even natural numbers = 
        22 + 42 + .... + (2n)2
      = 4 * (12 + 22 + .... + n2)
      = 4 * n(n+1)(2n+1) / 6
      = 2 * n(n+1)(2n+1)/3 
C++
Java
Python3
C#
PHP
Javascript
// Efficient C++ method to find sum// of square of first n even numbers.#include <iostream>using namespace std; int squareSum(int n){    return 2 * n * (n + 1) *            (2 * n + 1) / 3;} // Driver codeint main(){    cout << squareSum(8);    return 0;}
// Efficient Java method to find sum// of square of first n even numbers.import java.io.*; class GFG{    static int squareSum(int n)    {        return 2 * n * (n + 1) *                (2 * n + 1) / 3;    }     // Driver Code    public static void main(String args[])                    throws IOException    {        System.out.println(squareSum(8));    }} // This code is contributed by Nikita Tiwari
# Efficient Python3 code to# find sum of square of# first n even numbers def squareSum( n ):         return int(2 * n * (n + 1) * (2 * 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 even numbers.using System; class GFG{         static int squareSum(int n)    {        return 2 * n * (n + 1) * (2 * 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 even numbers.  function squareSum( $n){    return 2 * $n * ($n + 1) *             (2 * $n + 1) / 3;} echo squareSum(8); // This code is contributed by vt_m.?>
<script> // JavaScript program to find sum of// square of first n even numbers.     function squareSum(n)    {        let sum = 0;        for (let i = 1; i <= n; i++)            sum += (2 * i) * (2 * i);        return sum;    } // Driver code             document.write(squareSum(8)); </script>
Output : 
816
 
vt_m
souravmahato348
code_hunt
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.
Comments
Old Comments
Program to find sum of elements in a given array
Modulo Operator (%) in C/C++ with Examples
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Merge two sorted arrays
Euclidean algorithms (Basic and Extended)
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value