Program for Sum of Cubes of n Natural Numbers

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

Overview

This exercise will demonstrate how to obtain the sum of cubes of n natural numbers. Here, a single for loop from 1 to n is being used. We compute the term's cube in each step before adding it to the total. This program executes in O(n) time. However, we may use this series formula given below to solve it in O(1) or constant time.

Sum of cube if n natural number

The formula for the sum of cubes of the first n even natural numbers is:

Sum=n(n+1)(2n+1)/6Sum = n(n + 1)(2n + 1)/6

The formula for the sum of cubes of the first n odd natural numbers is:

Sum=n2(n21)Sum = n^2 * (n^2 - 1)

The Sum of the Cube of First n Natural Numbers

Print the sum of series 13+23+33+43+.+n31^3 + 2^3 + 3^3 + 4^3 + …….+ n^3 till n-th term.

Example

Examples of the series are as follows:

Input: n = 2
Output: 9

13+23=91^3 + 2^3 = 9

Input: n = 5
Output: 225

13+23+33+43+53=2251^3 + 2^3 + 3^3 + 4^3 + 5^3 = 225

Solution:

A simple solution is to add terms one by one.

Implementation

Adding Terms One by One

Simple C++ program to find the sum of series with cubes of first n natural numbers:

  1. Initialize a variable to store the sum and iterate from 1 to n, adding the cube of each natural number to the sum in each iteration.
  2. After the loop finishes, the sum will contain the result of the series sum of the cubes of the first n natural numbers.

Simple Java program to find the sum of series with cubes of first n natural numbers:

Simple Python program to find the sum of series with cubes of first n natural numbers:

Output:

  • Time Complexity: O(n)
  • Auxiliary Space: O(1)

Using Formula

The direct mathematical formula

(n(n+1)/2)(n (n + 1) / 2)

2 is an effective way to solve the problem.

For n = 5 sum by formula is:

(5(5+1)/2))2=(56/2)2=(15)2=225 (5*(5 + 1 ) / 2)) ^ 2 = (5*6/2) ^ 2 = (15) ^ 2 = 225

For n = 7, sum by formula is:

(7(7+1)/2))2=(78/2)2=(28)2=784 (7*(7 + 1 ) / 2)) ^ 2 = (7*8/2) ^ 2 = (28) ^ 2 = 784

A formula-based C++ program to find the sum of a series with cubes of first n natural numbers:

A formula-based Java program to find the sum of series with cubes of first n natural numbers:

A formula-based Python program to find the sum of series with cubes of first n natural numbers:

Output:

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

Program to Find Sum of Cube of N Even Natural Numbers

Find the Sum of the First n Even Natural Numbers of Given n.

Examples:

Input: 2
Output: 72

23+43=722^3 + 4^3 = 72

Input: 8
Output: 10368

23+43+63+83+103+123+143+163=103682^3 + 4^3 + 6^3 + 8^3 + 10^3 + 12^3 + 14^3 + 16^3 = 10368

Finding the sum of the cubes by iterating through n even numbers is easy.

Simple C++ method to find the sum of cubes of first n even numbers:

Java program to perform sum of cubes of first n even natural numbers:

Python3 program to find the sum of cubes of first n even numbers:

Output

  • Time Complexity: O(n)
  • Auxiliary Space: O(1)

To solve the problem effectively, use the formula below.

We are aware that n2(n+1)2/4 n2(n+1)2 / 4 is the sum of the cubes of the first n natural numbers.

Sum of cubes of first n natural numbers=

23+43+....+(2n)3=8(13+23+....+n3)=8n2(n+1)2/4=2n2(n+1)2 2^3 + 4^3 + .... + (2n)^3 = 8 * (1^3 + 2^3 + .... + n^3) = 8 * n2(n+1)2 / 4 = 2 * n2(n+1)2

Example

Efficient C++ method to find the sum of cubes of first n even numbers:

Java program to perform sum of cubes of first n even natural numbers:

Python3 program to find the sum of cubes of first n even numbers:

Output:

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

Program to Find Sum of Cube of N Odd Natural Numbers

Find the Sum of the First n Odd Natural Numbers Given n.

Input: 2
Output: 28

13+33=281^3 + 3^3 = 28

Input: 4
Output: 496

13+33+53+73=4961^3 + 3^3 + 5^3 + 7^3 = 496

Finding the sum of the cubes by iterating through n odd numbers is an easy solution.

Simple C++ method to find the sum of cubes of first n odd numbers:

Java program to perform sum of cubes of first n odd natural numbers:

Python3 program to find sum of cubes of first n odd numbers:

C# program to perform sum of cubes of first n odd natural numbers:

Output

Complexity Analysis

Time Complexity: O(n), since the cubeSum() function only does one traverse.

Space Complexity: O(1)

Applying the formula below will result in an effective solution.

sum=n2(2n21)sum = n^2 (2n^2 - 1)

How Does It Function?

We are aware that

n2(n+1)2/4n2(n+1)2 / 4

is the sum of the cubes of the first n natural numbers.

Sum of cubes of first n odd natural numbers = Sum of cubes of first 2n natural numbers - Sum of cubes of first n even natural numbers

=(2n)2(2n+1)2/42n2(n+1)2=n2(2n+1)22n2(n+1)2=n2[(2n+1)22(n+1)2]=n2(2n21) = (2n)2(2n+1)2 / 4 - 2 * n2(n+1)2 = n2(2n+1)2 - 2 * n2(n+1)2 = n2[(2n+1)2 - 2*(n+1)2] = n2(2n2 - 1)

Efficient C++ method to find sum of cubes of first n odd numbers:

Java program to perform sum of cubes of first n odd natural numbers:

Python3 program to find the sum of cubes of first n odd numbers:

Output:

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

FAQs

Q. What is the sum of the first n natural number cubes?

A. The counting of numbers that begin with 1 and proceed to infinity is known as natural numbers. Finding the sum of the cubes of the first n natural numbers involves combining the cubes of a given number of natural integers beginning with 1. For instance, you can write the sum of the cubes of the first five natural numbers as

13+23+33+43+5313 + 23 + 33 + 43 + 53

, the first ten natural numbers as

13+23+33+43+53+63+73+83+93+10313 + 23 + 33 + 43 + 53 + 63 + 73 + 83 + 93 + 103

, and so on.

Q. What is the formula for the sum of cubes of n natural numbers?

A. The formula for the sum of cubes of n natural integers is as follows: The formula to calculate the sum is,

13+23+33+43+...+n313 + 23 + 33 + 43 +... + n3

if we have a series of cubes of n natural numbers.

Formula for the sum of cubes of n natural integers

where n is the total number of natural numbers, counting from 1, that have been used.

Q. Pseudo code or steps calculating the sum of cubes of n natural numbers?

A. We will use a simple iterative method in which any loop, such as a for-loop, while-loop, or do-while loop, may be used:

  • From 1 to n times, iterate i.
  • For every i find it’s a cube.
  • Continue to add each cube to the sum variable.
  • Return the sum variable.
  • Print the result.

Conclusion

  • A program to find the sum of cubes of n natural numbers is a common computational task that involves summing the cubes of integers from 1 to n.
  • It can be implemented using a simple loop structure, typically a for or while loop, to iterate through the natural numbers from 1 to n.
  • For each natural number in the range, its cube is calculated and added to a running sum variable, which stores the cumulative sum of cubes.
  • The loop continues until all natural numbers from 1 to n have been processed, resulting in the sum of the cubes.
  • Efficient algorithms can optimize this calculation by using the formula for the sum of cubes, which reduces the time complexity from O(n) to O(1) for finding the sum of cubes of the first n natural numbers.