Python Compute the Sum of Cubes of the First n Natural Numbers
Formula 13 + 23 + 33 + 43 + …….+ n3
Requirements:
Input : n = 5
Output : 225
Formula : 13 + 23 + 33 + 43 + 53 = 225
Input : n = 7
Input : 784
Formula : 13 + 23 + 33 + 43 + 53 + 63 + 73 = 784
Example
Define the function for the sum of cubes
def
sumOfSeries
(
n
)
:
sum
=
0
for
i
in
range
(
1
,
n+
1
)
:
sum
=
iii
return
sum
Calling a function
n
=
5
(
sumOfSeries
(
n
)
)
The output of the above example is:
225
Python3 Examples