Java Program for cube sum of first n natural numbers
Last Updated :
19 Oct, 2023
Write a Java program to print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till the n-th term.
Examples:
Input: n = 5
Output: 225
Explanation: 13 + 23 + 33 + 43 + 53 = 225
Input: n = 7
Output: 784
Explanation: 13 + 23 + 33 + 43 + 53 + 63 + 73 = 784
Java Program for cube sum of first n natural numbers using Naive Approach:
- Define the function sumOfCubes() which takes an integer input n.
- Declare and initialize a variable sum to 0.
- Start a loop with i ranging from 1 to n.
- For each iteration of the loop, calculate the cube of i and add it to sum.
- After the loop ends, return the value of sum.
Below is the implementation of the above approach:
Java
// Simple Java program to find sum of series
// with cubes of first n natural numbers
import java.util.*;
import java.lang.*;
class GFG {
/* Returns the sum of series */
public static int sumOfSeries(int n)
{
int sum = 0;
for (int x = 1; x <= n; x++)
sum += x * x * x;
return sum;
}
// Driver Function
public static void main(String[] args)
{
int n = 5;
System.out.println(sumOfSeries(n));
}
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>
Time complexity: O(n), where n is the input value.
Auxiliary space: O(1),
Java Program for cube sum of first n natural numbers using Mathematical Formula:
An efficient solution is to use direct mathematical formula which is (n ( n + 1 ) / 2) ^ 2.
How does this formula work?
We can prove the formula using mathematical induction. We can easily see that the formula holds true for n = 1 and n = 2. Let this be true for n = k-1.
Let the formula be true for n = k-1.
Sum of first (k-1) natural numbers =
[((k - 1) * k)/2]2
Sum of first k natural numbers =
= Sum of (k-1) numbers + k3
= [((k - 1) * k)/2]2 + k3
= [k2(k2 - 2k + 1) + 4k3]/4
= [k4 + 2k3 + k2]/4
= k2(k2 + 2k + 1)/4
= [k*(k+1)/2]2
Illustration:
For n = 5 sum by formula is
(5*(5 + 1 ) / 2)) ^ 2
= (5*6/2) ^ 2
= (15) ^ 2
= 225
For n = 7, sum by formula is
(7*(7 + 1 ) / 2)) ^ 2
= (7*8/2) ^ 2
= (28) ^ 2
= 784
Below is the implementation of the above approach:
Java
// A formula based Java program to find sum
// of series with cubes of first n natural
// numbers
import java.util.*;
import java.lang.*;
class GFG {
/* Returns the sum of series */
public static int sumOfSeries(int n)
{
int x = (n * (n + 1) / 2);
return x * x;
}
// Driver Function
public static void main(String[] args)
{
int n = 5;
System.out.println(sumOfSeries(n));
}
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>
Time Complexity : O(1)
Auxiliary Space : O(1) because constant space has been used.
The above program causes overflow, even if result is not beyond integer limit. Like previous post, we can avoid overflow upto some extent by doing division first.
Below is the implementation of the above approach:
Java
// Efficient Java program to find sum of cubes
// of first n natural numbers that avoids
// overflow if result is going to be with in
// limits.
import java.util.*;
import java.lang.*;
class GFG {
/* Returns the sum of series */
public static int sumOfSeries(int n)
{
int x;
if (n % 2 == 0)
x = (n / 2) * (n + 1);
else
x = ((n + 1) / 2) * n;
return x * x;
}
// Driver Function
public static void main(String[] args)
{
int n = 5;
System.out.println(sumOfSeries(n));
}
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>
Time complexity: O(1) since performing constant operations
Auxiliary Space: O(1)
Please refer complete article on Program for cube sum of first n natural numbers for more details!
Similar Reads
Program for cube sum of first n natural numbers Print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till n-th term.Examples : Input : n = 5 Output : 225 13 + 23 + 33 + 43 + 53 = 225 Input : n = 7 Output : 784 13 + 23 + 33 + 43 + 53 + 63 + 73 = 784 Recommended PracticeSum of first n terms Try It! A simple solution is to one by one add terms. C
9 min read
C Program for cube sum of first n natural numbers Write a C program to print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till the n-th term. Examples: Input: n = 5Output: 225Explanation: 13 + 23 + 33 + 43 + 53 = 225 Input: n = 7Output: 784Explanation: 13 + 23 + 33 + 43 + 53 + 63 + 73 = 784 C Program for cube sum of first n natural numbers usi
4 min read
C++ Program for cube sum of first n natural numbers Write a C++ program to print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till the n-th term. Examples: Input: n = 5Output: 225Explanation: 13 + 23 + 33 + 43 + 53 = 225 Input: n = 7Output: 784Explanation: 13 + 23 + 33 + 43 + 53 + 63 + 73 = 784 C++ Program for cube sum of first n natural numbers
4 min read
Java Program for Sum of squares of first n natural numbers Given a positive integer N. The task is to find 12 + 22 + 32 + ..... + N2.Examples: Input : N = 4 Output : 30 12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30 Input : N = 5 Output : 55 Method 1: O(N) The idea is to run a loop from 1 to n and for each i, 1 <= i <= n, find i2 to sum. Java // Java Program
5 min read
Sum of cubes of first n odd natural numbers Given a number n, find sum of first n odd natural numbers. Input : 2 Output : 28 1^3 + 3^3 = 28 Input : 4 Output : 496 1^3 + 3^3 + 5^3 + 7^3 = 496 A simple solution is to traverse through n odd numbers and find the sum of cubes. C++ // Simple C++ method to find sum of cubes of // first n odd numbers
5 min read
Sum of alternating sign cubes of first N Natural numbers Given a number N, the task is to find the sum of alternating sign cubes of first N natural numbers, i.e., 13 - 23 + 33 - 43 + 53 - 63 + .... Examples: Input: N = 2 Output: -7 Explanation: Required sum = 13 - 23 = -7Input: N = 3 Output: 20 Explanation: Required sum = 13 - 23 + 33 = 20 Naive Approach:
6 min read
Sum of cubes of even and odd natural numbers We know that sum of cubes of first n natural numbers is = (n(n+1)/2)2. Sum of cube of first n even natural numbers 23 + 43 + 63 + ......... + (2n)3 Even Sum = 23 + 43 + 63 + .... + (2n)3 if we multiply by 23 then = 23 x (13 + 23 + 33 + .... + (n)3) = 23 + 43 + 63 + ......... + (2n)3 = 23 (n(n+1)/2)2
2 min read
Sum of cubes of first n even numbers Given a number n, find the sum of first n even natural numbers. Examples: Input : 2 Output : 72 2^3 + 4^3 = 72Input : 8 Output :10368 2^3 + 4^3 + 6^3 + 8^3 + 10^3 + 12^3 + 14^3 + 16^3 = 10368 A simple solution is to traverse through n even numbers and find the sum of cubes. C++ // Simple C++ method
5 min read
Perfect cube greater than a given number Given a number N, the task is to find the next perfect cube greater than N.Examples: Input: N = 6 Output: 8 8 is a greater number than 6 and is also a perfect cube Input: N = 9 Output: 27 Approach: Find the cube root of given N.Calculate its floor value using floor function in C++.Then add 1 to it.P
3 min read
Print N numbers such that their product is a Perfect Cube Given a number N, the task is to find distinct N numbers such that their product is a perfect cube.Examples: Input: N = 3 Output: 1, 8, 27 Explanation: Product of the output numbers = 1 * 8 * 27 = 216, which is the perfect cube of 6 (63 = 216)Input: N = 2 Output: 1 8 Explanation: Product of the outp
7 min read