Sum of cubes of first n odd natural numbers
Given a number n, find sum of first n odd natural numbers.
Input : 2
Output : 28
1^3 + 3^3 = 28
Input : 4
Output : 496
1^3 + 3^3 + 5^3 + 7^3 = 496
A simple solution is to traverse through n odd numbers and find the sum of cubes.
C++
#include <iostream>
using namespace std;
int cubeSum( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += (2*i + 1)*(2*i + 1)*(2*i + 1);
return sum;
}
int main()
{
cout << cubeSum(2);
return 0;
}
|
Java
public class GFG
{
public static int cubesum( int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += ( 2 * i + 1 ) * ( 2 * i + 1 )
* ( 2 * i + 1 );
return sum;
}
public static void main(String args[])
{
int a = 5 ;
System.out.println(cubesum(a));
}
}
|
Python3
def cubeSum(n):
sum = 0
for i in range ( 0 , n) :
sum + = ( 2 * i + 1 ) * ( 2 * i + 1 ) * ( 2 * i + 1 )
return sum
print (cubeSum( 2 ))
|
C#
using System;
public class GFG
{
public static int cubesum( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += (2 * i + 1) * (2 * i +1)
* (2 * i + 1);
return sum;
}
public static void Main()
{
int a = 5;
Console.WriteLine(cubesum(a));
}
}
|
PHP
<?php
function cubeSum( $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += (2 * $i + 1) *
(2 * $i + 1) *
(2 * $i + 1);
return $sum ;
}
echo cubeSum(2);
?>
|
Javascript
<script>
function cubeSum( n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum += (2*i + 1)*(2*i + 1)*(2*i + 1);
return sum;
}
document.write(cubeSum(2));
</script>
|
Output :
28
Complexity Analysis:
Time Complexity: O(n), as we are using a single traversal in the cubeSum() function.
Space Complexity:O(1)
An efficient solution is to apply the below formula.
sum = n2(2n2 - 1)
How does it work?
We know that sum of cubes of first
n natural numbers is = n2(n+1)2 / 4
Sum of first n even numbers is 2 * n2(n+1)2
Sum of cubes of first n odd natural numbers =
Sum of cubes of first 2n natural numbers -
Sum of cubes of first n even natural numbers
= (2n)2(2n+1)2 / 4 - 2 * n2(n+1)2
= n2(2n+1)2 - 2 * n2(n+1)2
= n2[(2n+1)2 - 2*(n+1)2]
= n2(2n2 - 1)
C++
#include <iostream>
using namespace std;
int cubeSum( int n)
{
return n * n * (2 * n * n - 1);
}
int main()
{
cout << cubeSum(4);
return 0;
}
|
Java
public class GFG
{
public static int cubesum( int n)
{
return (n) * (n) * ( 2 * n * n - 1 );
}
public static void main(String args[])
{
int a = 4 ;
System.out.println(cubesum(a));
}
}
|
Python3
def cubeSum(n):
return (n * n * ( 2 * n * n - 1 ))
print (cubeSum( 4 ))
|
C#
using System;
public class GFG
{
public static int cubesum( int n)
{
return (n) * (n) * (2 * n * n - 1);
}
public static void Main()
{
int a = 4;
Console.WriteLine(cubesum(a));
}
}
|
PHP
<?php
function cubeSum( $n )
{
return $n * $n * (2 * $n * $n - 1);
}
echo cubeSum(4);
?>
|
Javascript
<script>
function cubesum(n)
{
return (n) * (n) * (2 * n * n - 1);
}
var a = 4;
document.write(cubesum(a));
</script>
|
Output:
496
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(1)
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...