Python calculate the sum of cubes of n natural numbers

Document 对象参考手册Python3 Examples

Calculation formula 13 + 23 + 33 + 43 + …….+ n3

Implementation 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 sum of cubes
def sumOfSeries(n):
    sum = 0
    for i in range(1, n+1):
        sum +=i*i*i
         
    return sum
 
   
# Call the function
n = 5
print(sumOfSeries(n))

The output result of the above example is:

225

Document 对象参考手册Python3 Examples

Other extensions