Python Program to Find Sum of Cubes of n Numbers
You can find the sum of cubes of n numbers in Python by using a loop to cube each number and add them, like
sum_of_cubes = sum(i**3 for i in range(1, n+1)).Examples
Inputn = 1
Output1
Inputn = 3
Output36
Inputn = 0
Output0
How to Think About It
To find the sum of cubes of n numbers, think of each number from 1 to n. Cube each number by multiplying it by itself twice, then add all these cubes together to get the total sum.
Algorithm
1
Get the input number n.2
Initialize a variable to hold the sum as 0.3
For each number from 1 to n, calculate its cube by multiplying the number by itself twice.4
Add each cube to the sum variable.5
After the loop ends, return or print the sum.Code
python
n = int(input("Enter the number of terms: ")) sum_of_cubes = 0 for i in range(1, n+1): sum_of_cubes += i**3 print("Sum of cubes:", sum_of_cubes)
Output
Enter the number of terms: 3
Sum of cubes: 36
Dry Run
Let's trace the program for n = 3 through the code
1
Input
n = 3
2
Initialize sum
sum_of_cubes = 0
3
First iteration (i=1)
sum_of_cubes = 0 + 1**3 = 1
4
Second iteration (i=2)
sum_of_cubes = 1 + 2**3 = 1 + 8 = 9
5
Third iteration (i=3)
sum_of_cubes = 9 + 3**3 = 9 + 27 = 36
6
Output
Print sum_of_cubes = 36
| i | i^3 | sum_of_cubes |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 8 | 9 |
| 3 | 27 | 36 |
Why This Works
Step 1: Loop through numbers
We use a loop to go through each number from 1 to n to handle all numbers one by one.
Step 2: Cube each number
Cubing a number means multiplying it by itself twice, done here with i**3.
Step 3: Add cubes to sum
Each cube is added to a running total to get the final sum of all cubes.
Alternative Approaches
Using sum with generator expression
python
n = int(input("Enter the number of terms: ")) sum_of_cubes = sum(i**3 for i in range(1, n+1)) print("Sum of cubes:", sum_of_cubes)
This method is shorter and uses Python's built-in sum function with a generator for better readability.
Using formula for sum of cubes
python
n = int(input("Enter the number of terms: ")) sum_of_cubes = (n*(n+1)//2)**2 print("Sum of cubes:", sum_of_cubes)
This uses the mathematical formula that the sum of cubes of first n numbers equals the square of the sum of first n numbers, which is very efficient.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through all numbers from 1 to n once, so it takes linear time O(n).
Space Complexity
Only a few variables are used regardless of n, so space complexity is constant O(1).
Which Approach is Fastest?
Using the formula approach is fastest with O(1) time, while looping methods take O(n).
| Approach | Time | Space | Best For |
|---|---|---|---|
| Loop with for | O(n) | O(1) | Simple understanding and small n |
| Sum with generator | O(n) | O(1) | Cleaner code for moderate n |
| Mathematical formula | O(1) | O(1) | Large n and best performance |
Use the formula (n*(n+1)//2)**2 for the fastest calculation of sum of cubes.
Forgetting to include the last number n in the range by using range(1, n) instead of range(1, n+1).