In the previous article, we have discussed Python Program to Find Isosceles Triangle Area
The general mathematical formula to calculate the sum of cubes of a series = ( n (n+1) / 6)²
where n= nth term of a series.
Given the nth term of a series and the task is to calculate the sum of the cubes of a series.
Math Module :
Python’s math module is a built-in module. By importing this module, we can perform mathematical computations.
Numerous mathematical operations like ceil( ),floor( ),factorial( ),mod( ),value of pi ,…..etc .can be computed with the help of math module.
math.pow() :
The math. pow() method returns the value of x to the power of y.
It throws a ValueError if x is negative and y is not an integer.
Both arguments are converted to floats by this method.
Note: Using math.pow(1.0,x) or math.pow(x,0.0) always returns 1.0.
Examples:
Example1:
Input:
Given n value = 13
Output:
The sum of cubes of a series with the given n value[ 13 ] = 8281.0
Example2:
Input:
Given n value = 6
Output:
The sum of cubes of a series with the given n value[ 6 ] = 441.0
Program to Calculate Sum of Series 1³+2³+3³+….+n³
Below are the ways to calculate the sum of the cubes of a given series.
Method #1: Using Mathematical Formula (Static Input)
Approach:
- Import math module using the import keyword.
- Give the nth term of a series () as static input and store it in a variable.
- Calculate the sum of cubes of a series with a given nth term using the above mathematical formula and math.pow() function.
- Store it in another variable.
- Print the sum of cubes of a given series with the given n value.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the nth term of a series as static input and store it in a variable. num = 10 # Calculate the sum of cubes of a series with a given nth term using the above # mathematical formula and math.pow() function. # Store it in another variable. cube_sum = math.pow((num * (num + 1)) / 2, 2) # Print the sum of cubes of a given series with the given n value. print( "The sum of cubes of a series with the given n value[", num, "] =", cube_sum)
Output:
The sum of cubes of a series with the given n value[ 10 ] = 3025.0
Method #2: Using Mathematical Formula (User Input)
Approach:
- Import math module using the import keyword.
- Give the nth term of a series as user input using the int(input()) function and store it in a variable.
- Calculate the sum of cubes of a series with a given nth term using the above mathematical formula and math.pow() function.
- Store it in another variable.
- Print the sum of cubes of a given series with the given n value.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the nth term of a series as user input using the int(input()) function and store it in a variable. num = int(input("Enter some random variable = ")) # Calculate the sum of cubes of a series with a given nth term using the above # mathematical formula and math.pow() function. # Store it in another variable. cube_sum = math.pow((num * (num + 1)) / 2, 2) # Print the sum of cubes of a given series with the given n value. print( "The sum of cubes of a series with the given n value[", num, "] =", cube_sum)
Output:
Enter some random variable = 8 The sum of cubes of a series with the given n value[ 8 ] = 1296.0
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.