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. #include <iostream> using namespace std; int cubeSum(int n) { int sum = 0; for (int i = 0; i < n; i++) sum += (2*i + 1)*(2*i + 1)*(2*i + 1); return sum; } int main() { cout << cubeSum(2); return 0; } |
chevron_right
filter_none
Java
// Java program to perform sum of // cubes of first n odd natural numbers public class GFG { public static int cubesum(int n) { int sum = 0; for(int i = 0; i < n; i++) sum += (2 * i + 1) * (2 * i +1) * (2 * i + 1); return sum; } // Driver function public static void main(String args[]) { int a = 5; System.out.println(cubesum(a)); } } // This article is published Akansh Gupta |
chevron_right
filter_none
Python3
# Python3 program to find sum of # cubes of first n odd numbers. def cubeSum(n): sum = 0 for i in range(0, n) : sum += (2 * i + 1) * (2 * i + 1) * (2 * i + 1) return sum # Driven code print(cubeSum(2)) # This code is contributed by Shariq Raza |
chevron_right
filter_none
C#
// C# program to perform sum of // cubes of first n odd natural numbers using System; public class GFG { public static int cubesum(int n) { int sum = 0; for(int i = 0; i < n; i++) sum += (2 * i + 1) * (2 * i +1) * (2 * i + 1); return sum; } // Driver function public static void Main() { int a = 5; Console.WriteLine(cubesum(a)); } } // This code is published vt_m |
chevron_right
filter_none
PHP
<?php // Simple PHP method to find sum of // cubes of first n odd numbers. function cubeSum($n) { $sum = 0; for ($i = 0; $i < $n; $i++) $sum += (2 * $i + 1) * (2 * $i + 1) * (2 * $i + 1); return $sum; } // Driver Code echo cubeSum(2); // This code is contributed by vt_m. ?> |
chevron_right
filter_none
Output :
28
An efficient solution is to apply below formula.
sum = n2(2n2 - 1) How does it work? We know that sum of cubes of first n natural numbers is = n2(n+1)2 / 4 Sum of first n even numbers is 2 * n2(n+1)2 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 / 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)
C++
// Efficient C++ method to find sum of cubes of // first n odd numbers. #include <iostream> using namespace std; int cubeSum(int n) { return n * n * (2 * n * n - 1); } int main() { cout << cubeSum(4); return 0; } |
chevron_right
filter_none
Java
// Java program to perform sum of // cubes of first n odd natural numbers public class GFG { public static int cubesum(int n) { return (n) * (n) * (2 * n * n - 1); } // Driver function public static void main(String args[]) { int a = 4; System.out.println(cubesum(a)); } } // This code is contributed by Akansh Gupta. |
chevron_right
filter_none
Python3
# Python3 program to find sum of # cubes of first n odd numbers. # Function to find sum of cubes # of first n odd number def cubeSum(n): return (n * n * (2 * n * n - 1)) # Driven code print(cubeSum(4)) # This code is contributed by Shariq Raza |
chevron_right
filter_none
C#
// C# program to perform sum of // cubes of first n odd natural numbers using System; public class GFG { public static int cubesum(int n) { return (n) * (n) * (2 * n * n - 1); } // Driver function public static void Main() { int a = 4; Console.WriteLine(cubesum(a)); } } // This code is published vt_m. |
chevron_right
filter_none
PHP
<?php // Efficient PHP method to // find sum of cubes of // first n odd numbers. function cubeSum($n) { return $n * $n * (2 * $n * $n - 1); } // Driver Code echo cubeSum(4); // This code is contributed by vt_m. ?> |
chevron_right
filter_none
Output:
496
This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Sum of cubes of even and odd natural numbers
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of cubes of first n even numbers
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Sum of sum of first n natural numbers
- Natural Numbers
- LCM of First n Natural Numbers
- Sum of squares of first n natural numbers
- Sum of natural numbers using recursion
- Sum of first N natural numbers which are divisible by X or Y
- Repeated sum of first N natural numbers
- Sum of kth powers of first n natural numbers
- Sum of all odd natural numbers in range L and R
- Sum of all natural numbers in range L to R
- Sum of first N natural numbers which are not powers of K
Improved By : vt_m



