VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-of-cubes-of-first-n-even-numbers/

⇱ Sum of cubes of first n even numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of cubes of first n even numbers

Last Updated : 16 Feb, 2023

Given a number n, find the sum of first n even natural numbers. 

Examples: 

Input : 2
Output : 72
2^3 + 4^3 = 72
Input : 8
Output :10368
2^3 + 4^3 + 6^3 + 8^3 + 10^3 + 12^3 + 14^3 + 16^3 = 10368

A simple solution is to traverse through n even numbers and find the sum of cubes.  

Output:

10368

Time Complexity: O(n)
Auxiliary Space: O(1)

An efficient solution is to apply below formula. 

sum = 2 * n2(n+1)2 
 

How does it work? 

We know that sum of cubes of first 
n natural numbers is = n2(n+1)2 / 4

Sum of cubes of first n natural numbers = 
 2^3 + 4^3 + .... + (2n)^3
 = 8 * (1^3 + 2^3 + .... + n^3)
 = 8 * n2(n+1)2 / 4
 = 2 * n2(n+1)2 

Example

Output:

10368

Time Complexity: O(1)

Auxiliary Space: O(1)

Sum of cube of first n odd natural numbers

Comment