Sum of square of first n even numbers
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++
// 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 Code int main() { cout << squareSum(8); return 0; } |
chevron_right
filter_none
Java
// 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 |
chevron_right
filter_none
Python3
# 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 code ans = squareSum(8) print (ans) # This code is contributed by Saloni Gupta |
chevron_right
filter_none
C#
// 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. |
chevron_right
filter_none
PHP
<?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. ?> |
chevron_right
filter_none
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++
// 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 code int main() { cout << squareSum(8); return 0; } |
chevron_right
filter_none
Java
// 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 |
chevron_right
filter_none
Python3
# 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 code ans = squareSum(8) print (ans) # This code is contributed by Saloni Gupta |
chevron_right
filter_none
C#
// 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. |
chevron_right
filter_none
PHP
<?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. ?> |
chevron_right
filter_none
Output :
816
Recommended Posts:
- Count square and non-square numbers before n
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Sum of square of first n odd numbers
- Print n numbers such that their sum is a perfect square
- Program to print non square numbers
- Sum of square-sums of first n natural numbers
- Difference between sum of the squares of first n natural numbers and square of sum
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Check if product of array containing prime numbers is a perfect square
- Check if a number is perfect square without finding square root
- Count numbers upto N which are both perfect square and perfect cube
- Magic Square
- Nth non-Square number
- Square root of a number using log
- Lagrange's four square theorem
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 : vt_m



