Given an integer n, Find the sum of series 13 + 23 + 33 + 43 + ... + n3 till n-th term.
Examples:
Input: n = 5
Output: 225
Explanation: 13 + 23 + 33 + 43 + 53 = 225Input: n = 7
Output: 784
Explanation: 13 + 23 + 33 + 43 + 53 + 63 + 73 = 784
Table of Content
[Naive Approach] Iterative Method - O(n) Time and O(1) Space
Iterate from 1 to n and keep adding the cube of each number to a running sum. Finally, return the accumulated sum.
Dry run for n=5:
- Initialize sum = 0
- For i = 1, add (1^3 = 1), sum becomes 1
- For i = 2, add (2^3 = 8), sum becomes 9
- For i = 3, add (3^3 = 27), sum becomes 36
- For i = 4, add (4^3 = 64), sum becomes 100
- For i = 5, add (5^3 = 125), sum becomes 225
Final result = 225
#include <iostream>
using namespace std;
int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i * i * i;
return sum;
}
int main()
{
int n = 5;
cout << sumOfSeries(n);
return 0;
}
import java.util.*;
import java.lang.*;
class GFG {
public static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i * i * i;
return sum;
}
public static void main(String[] args)
{
int n = 5;
System.out.println(sumOfSeries(n));
}
}
def sumOfSeries(n):
sum = 0
for i in range(1, n + 1):
sum += i * i*i
return sum
if __name__ == "__main__":
n = 5
print(sumOfSeries(n))
using System;
class GFG {
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i * i * i;
return sum;
}
public static void Main()
{
int n = 5;
Console.Write(sumOfSeries(n));
}
}
function sumOfSeries( n)
{
let sum = 0;
for (let i = 1; i <= n; i++)
sum += i * i * i;
return sum;
}
// Driver Code
let n = 5;
console.log(sumOfSeries(n));
Output
225
[Expected Approach] Formula Based Method- O(1) Time and O(1) Space
Use the direct formula (n(n+1)/2)^2 to compute the sum without iteration.

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
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
#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;
}
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));
}
}
def sumOfSeries(n):
x = (n * (n + 1) / 2)
return (int)(x * x)
if __name__ == "__main__":
n = 5
print(sumOfSeries(n))
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));
}
}
/* Returns the sum of series */
function sumOfSeries( n)
{
let x = (n * (n + 1) / 2)
return (x * x)
}
// Driver Code
let n = 5;
console.log(sumOfSeries(n));
Output
225
Note: The expression (n*(n+1)/2)^2 may overflow before division for large n.
Rearrange the computation by performing division before multiplication to reduce intermediate values and minimize the risk of integer overflow.
#include <iostream>
using namespace std;
int sumOfSeries(int n)
{
int x;
// Division before multiplication
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;
}
import java.util.*;
class GFG {
public static int sumOfSeries(int n)
{
int x;
// Division before multiplication
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));
}
}
def sumOfSeries(n):
x = 0
# Division before multiplication
if n % 2 == 0 :
x = (n / 2) * (n + 1)
else:
x = ((n + 1) / 2) * n
return (int)(x * x)
if __name__ == "__main__":
n = 5
print(sumOfSeries(n))
using System;
class GFG {
public static int sumOfSeries(int n)
{
int x;
// Division before multiplication
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));
}
}
function sumOfSeries( n)
{
let x=0
// Division before multiplication
if (n % 2 == 0)
x = (n / 2) * (n + 1)
else
x = ((n + 1) / 2) * n
return (x * x)
}
// Driver Code
let n = 5;
console.log(sumOfSeries(n));
Output:
225Time complexity: O(1)
Auxiliary Space: O(1)