
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Cube Sum of First N Natural Numbers in Java
In this article, we will learn to write a Java program to calculate the sum of cubes of the first n natural numbers.
Understanding the Cube Sum Formula
The sum of cubes of the first n natural numbers follows a mathematical formula ?
S = 13 + 23 + 33 + ... + n3 = { ( n ( n + 1 ) ) / 2 }2
Different Approaches
Following are the two different approaches to printing the cube sum of first n natural numbers ?
Using a Loop
The first approach involves iterating through the first n natural numbers, calculating the cube of each number, and adding it to a running sum.
Following are the steps to calculate the cube sum of the first n natural numbers ?
- The method first_n_nat_no takes an integer val as input, representing the number of natural numbers.
- A for loop iterates from 1 to val, calculating the cube of each number (x * x * x) and adding it to ini_sum.
- The final sum is returned and printed in the main method.
for (int x = 1; x <= val; x++) { ini_sum += x * x * x; // Cube of x and add to sum }
Example
Following is the Java code to cube the sum of the first n natural numbers ?
import java.util.*; import java.lang.*; public class Demo { public static int first_n_nat_no(int val) { int ini_sum = 0; for (int x = 1; x <= val; x++) { ini_sum += x * x * x; // Cube of x and add to sum } return ini_sum; } public static void main(String[] args) { int val = 7; System.out.println("The sum of cube of first 7 natural numbers is "); System.out.println(first_n_nat_no(val)); } }
Output
The sum of cube of first 7 natural numbers is 784
Time Complexity: O(n), time since it iterates through the numbers from 1 to n.
Space Complexity: O(1), as we only use a few integer variables.
Using the Mathematical Formula
The second approach uses a mathematical formula to calculate the sum of cubes of the first n natural numbers. The formula is:
The sum of cubes= { ( n ( n + 1 ) ) / 2 } 2
This formula is derived from the fact that the sum of cubes of the first n natural numbers is equal to the square of the sum of the first n natural numbers.
int sum = (val * (val + 1)) / 2; // Sum of first n natural numbers return sum * sum; // Square of the sum
Example
Following is the Java code to cube the sum of the first n natural numbers ?
import java.util.*; import java.lang.*; public class Demo { // Method to calculate the sum of cubes using the formula public static int first_n_nat_no_formula(int val) { int sum = (val * (val + 1)) / 2; // Sum of first n natural numbers return sum * sum; // Square of the sum } public static void main(String[] args) { int val = 7; // Number of natural numbers System.out.println("The sum of cube of first 7 natural numbers is "); System.out.println(first_n_nat_no_formula(val)); } }
Output
The sum of cube of first 7 natural numbers is 784
Time Complexity: O(1), (constant time) since it performs a fixed number of operations.
Space Complexity: O(1), as it uses only integer variables.
Conclusion
Both approaches are effective for calculating the sum of cubes of the first n natural numbers. The loop approach is straightforward and works well for small inputs, while the mathematical formula approach is highly efficient and recommended for larger inputs.