Last Updated : 23 Jul, 2025
We are given a number n and we need to print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till the n-th term in python.
Examples:
Input: n = 5
Output: 225
Explanation: 13 + 23 + 33 + 43 + 53 = 225
Let's discuss some of the ways to do it.
Using Mathematical Formula:Most efficient solution is to use the direct mathematical formula which is (n ( n + 1 ) / 2) ^ 2, where n is the number of terms.
Python
n = 5
res = ((n * (n + 1)) // 2) ** 2
print(res)
Explanation: ((n * (n + 1)) // 2) ** 2 gives us the sum of cubes of integers from 1 to n.
Using Brute Force approachWe can iterate from 1 to n, computing the cube of each number and summing them, where n is the limit up to which the sum is required.
Python
n = 5
sum = 0
for i in range(1, n + 1):
res += i ** 3
print(res)
Explanation: iterate from 1 to n using for loop and sum += i**3, keeps on adding the the cubes of the numbers.
Using generator expressionUse generator expression to generate a list of cubes for numbers from 1 to n, then apply the sum() function to get the total. This approach is concise and efficient, combining iteration and summation in a single line.
Python
n = 5
res = sum(i**3 for i in range(1, n + 1))
print(res)
Explanation: i**3 for i in range(1, n + 1) creates a list of cubes of numbers from 1 to n and then sum() function returns the sum of all the elements inside the list.
Using Enumerate ListIn this approach, we will use the enumerate list to find the cube sum of n natural numbers in one line.
Python
n = 5
res = sum([(i+1) ** 3 for i, _ in enumerate(range(n))])
print(res)
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4