Program for cube sum of first n natural numbers
Last Updated :
22 Jun, 2022
Print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till n-th term.
Examples :
Input : n = 5
Output : 225
13 + 23 + 33 + 43 + 53 = 225
Input : n = 7
Output : 784
13 + 23 + 33 + 43 + 53 +
63 + 73 = 784
A simple solution is to one by one add terms.
C++
#include <iostream>
using namespace std;
int sumOfSeries( int n)
{
int sum = 0;
for ( int x = 1; x <= n; x++)
sum += x * x * x;
return sum;
}
int main()
{
int n = 5;
cout << sumOfSeries(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
public static int sumOfSeries( int n)
{
int sum = 0 ;
for ( int x = 1 ; x <= n; x++)
sum += x * x * x;
return sum;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(sumOfSeries(n));
}
}
|
Python3
def sumOfSeries(n):
sum = 0
for i in range ( 1 , n + 1 ):
sum + = i * i * i
return sum
n = 5
print (sumOfSeries(n))
|
C#
using System;
class GFG {
static int sumOfSeries( int n)
{
int sum = 0;
for ( int x = 1; x <= n; x++)
sum += x * x * x;
return sum;
}
public static void Main()
{
int n = 5;
Console.Write(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries( $n )
{
$sum = 0;
for ( $x = 1; $x <= $n ; $x ++)
$sum += $x * $x * $x ;
return $sum ;
}
$n = 5;
echo sumOfSeries( $n );
?>
|
Javascript
<script>
function sumOfSeries( n)
{
let sum = 0;
for (let x = 1; x <= n; x++)
sum += x * x * x;
return sum;
}
let n = 5;
document.write(sumOfSeries(n));
</script>
|
Output :
225
Time Complexity: O(n)
Auxiliary Space: O(1)
An efficient solution is to use direct mathematical formula which is (n ( n + 1 ) / 2) ^ 2
For n = 5 sum by formula is
(5*(5 + 1 ) / 2)) ^ 2
= (5*6/2) ^ 2
= (15) ^ 2
= 225
For n = 7, sum by formula is
(7*(7 + 1 ) / 2)) ^ 2
= (7*8/2) ^ 2
= (28) ^ 2
= 784
C++
#include <iostream>
using namespace std;
int sumOfSeries( int n)
{
int x = (n * (n + 1) / 2);
return x * x;
}
int main()
{
int n = 5;
cout << sumOfSeries(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
public static int sumOfSeries( int n)
{
int x = (n * (n + 1 ) / 2 );
return x * x;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(sumOfSeries(n));
}
}
|
Python3
def sumOfSeries(n):
x = (n * (n + 1 ) / 2 )
return ( int )(x * x)
n = 5
print (sumOfSeries(n))
|
C#
using System;
class GFG {
public static int sumOfSeries( int n)
{
int x = (n * (n + 1) / 2);
return x * x;
}
public static void Main()
{
int n = 5;
Console.Write(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries( $n )
{
$x = ( $n * ( $n + 1) / 2);
return $x * $x ;
}
$n = 5;
echo sumOfSeries( $n );
?>
|
Javascript
<script>
function sumOfSeries( n)
{
x = (n * (n + 1) / 2)
return (x * x)
}
let n = 5;
document.write(sumOfSeries(n));
</script>
|
Output:
225
Time Complexity: O(1)
Auxiliary Space: O(1)
How does this formula work?
We can prove the formula using mathematical induction. We can easily see that the formula holds true for n = 1 and n = 2. Let this be true for n = k-1.
Let the formula be true for n = k-1.
Sum of first (k-1) natural numbers =
[((k - 1) * k)/2]2
Sum of first k natural numbers =
= Sum of (k-1) numbers + k3
= [((k - 1) * k)/2]2 + k3
= [k2(k2 - 2k + 1) + 4k3]/4
= [k4 + 2k3 + k2]/4
= k2(k2 + 2k + 1)/4
= [k*(k+1)/2]2
The above program causes overflow, even if result is not beyond integer limit. Like previous post, we can avoid overflow upto some extent by doing division first.
C++
#include <iostream>
using namespace std;
int sumOfSeries( int n)
{
int x;
if (n % 2 == 0)
x = (n / 2) * (n + 1);
else
x = ((n + 1) / 2) * n;
return x * x;
}
int main()
{
int n = 5;
cout << sumOfSeries(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
public static int sumOfSeries( int n)
{
int x;
if (n % 2 == 0 )
x = (n / 2 ) * (n + 1 );
else
x = ((n + 1 ) / 2 ) * n;
return x * x;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(sumOfSeries(n));
}
}
|
Python3
def sumOfSeries(n):
x = 0
if n % 2 = = 0 :
x = (n / 2 ) * (n + 1 )
else :
x = ((n + 1 ) / 2 ) * n
return ( int )(x * x)
n = 5
print (sumOfSeries(n))
|
C#
using System;
class GFG {
public static int sumOfSeries( int n)
{
int x;
if (n % 2 == 0)
x = (n / 2) * (n + 1);
else
x = ((n + 1) / 2) * n;
return x * x;
}
static public void Main ()
{
int n = 5;
Console.WriteLine(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries( $n )
{
$x ;
if ( $n % 2 == 0)
$x = ( $n / 2) * ( $n + 1);
else
$x = (( $n + 1) / 2) * $n ;
return $x * $x ;
}
$n = 5;
echo sumOfSeries( $n );
?>
|
Javascript
<script>
function sumOfSeries( n)
{
x=0
if (n % 2 == 0)
x = (n / 2) * (n + 1)
else
x = ((n + 1) / 2) * n
return (x * x)
}
let n = 5;
document.write(sumOfSeries(n));
</script>
|
Output:
225
Time complexity: O(1) since performing constant operations
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...