Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Compute the Sum of Cubes of the First n Natural Numbers

Python Compute the Sum of Cubes of the First n Natural Numbers

Document Object Reference Manual Python3 Examples

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

print

(

sumOfSeries

(

n

)

)

The output of the above example is:

225

Document Object Reference Manual Python3 Examples