Skip to content

Latest commit

 

History

History
57 lines (36 loc) · 1.54 KB

Dictionary-of-cubes.md

File metadata and controls

57 lines (36 loc) · 1.54 KB

9.2: Cube Sum

The Problem

With a given integral number n, write a program to calculate the sum of cubes.

Hints

Cubes mean power of 3. Hence, the cube of 9 is 9**3.

Here you have to calculate the cube of a series. If you want to calculate the cube up to the number n. The series will look like-

1^3+2^3+3^3+4^3+ .. .. .. +n^3

If you remembered the sum of the square, this one will be easier for you.

The solution

defcube_sum(num): sum=0forninrange(num+1): sum=sum+n**3returnsumuser_num=int(input('Enter a number: ')) result=cube_sum(user_num) print('Your sum of cubes are: ', result)

Try it on Programming Hero

Think Different

There is an alternative solution to calculate the sum of cube of the n numbers. You can use

(n*(n+1)/2)^2

Alternative Solution

n=int(input('Enter a number: ')) sum= (n*(n+1)/2)**2print('Your sum of cubes are: ', sum)

Try it on Programming Hero

Take Away

Sum of a series might have an easier formula.

  Next Page  

tags: programming-heropythonpython3problem-solvingprogrammingcoding-challengeinterviewlearn-pythonpython-tutorialprogramming-exercisesprogramming-challengesprogramming-fundamentalsprogramming-contestpython-coding-challengespython-problem-solving

close