Sum of square of first n even numbers
Last Updated :
22 Sep, 2022
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 = 816
A simple solution is to traverse through n even numbers and find the sum of square.
C++
#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;
}
int main()
{
cout << squareSum(8);
return 0;
}
|
Java
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;
}
public static void main(String args[])
throws IOException
{
System.out.println(squareSum( 8 ));
}
}
|
Python3
def squareSum( n ):
sum = 0
for i in range ( 0 , n + 1 ):
sum + = ( 2 * i) * ( 2 * i)
return sum
ans = squareSum( 8 )
print (ans)
|
C#
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;
}
public static void Main()
{
Console.Write(squareSum(8));
}
}
|
PHP
<?php
function squareSum( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum += (2 * $i ) * (2 * $i );
return $sum ;
}
echo squareSum(8);
?>
|
Javascript
<script>
function squareSum(n)
{
sum = 0;
for (let i = 1; i <= n; i++)
sum += (2 * i) * (2 * i);
return sum;
}
document.write(squareSum(8));
</script>
|
Output:
816
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 = 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++
#include <iostream>
using namespace std;
int squareSum( int n)
{
return 2 * n * (n + 1) *
(2 * n + 1) / 3;
}
int main()
{
cout << squareSum(8);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int squareSum( int n)
{
return 2 * n * (n + 1 ) *
( 2 * n + 1 ) / 3 ;
}
public static void main(String args[])
throws IOException
{
System.out.println(squareSum( 8 ));
}
}
|
Python3
def squareSum( n ):
return int ( 2 * n * (n + 1 ) * ( 2 * n + 1 ) / 3 )
ans = squareSum( 8 )
print (ans)
|
C#
using System;
class GFG
{
static int squareSum( int n)
{
return 2 * n * (n + 1) * (2 * n + 1) / 3;
}
public static void Main()
{
Console.Write(squareSum(8));
}
}
|
PHP
<?php
function squareSum( $n )
{
return 2 * $n * ( $n + 1) *
(2 * $n + 1) / 3;
}
echo squareSum(8);
?>
|
Javascript
<script>
function squareSum(n)
{
let sum = 0;
for (let i = 1; i <= n; i++)
sum += (2 * i) * (2 * i);
return sum;
}
document.write(squareSum(8));
</script>
|
Output :
816
Time complexity: O(1) since performing constant operations
Auxiliary Space: O(1) because constant extra space has been used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...