Dado un número n, encuentre la suma de los primeros n números naturales impares.
Input : 2 Output : 28 1^3 + 3^3 = 28 Input : 4 Output : 496 1^3 + 3^3 + 5^3 + 7^3 = 496
Una solución simple es atravesar n números impares y encontrar la suma de los cubos.
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; }
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
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
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
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. ?>
Javascript
<script> // Simple javascript method to find sum of cubes of // first n odd numbers. function cubeSum( n) { let sum = 0; for (let i = 0; i < n; i++) sum += (2*i + 1)*(2*i + 1)*(2*i + 1); return sum; } document.write(cubeSum(2)); // This code is contributed by Rajput-Ji </script>
Producción :
28
Análisis de Complejidad:
Complejidad de tiempo: O(n), ya que estamos usando un solo recorrido en la función cubeSum().
Complejidad espacial:O(1)
Una solución eficiente es aplicar la siguiente fórmula.
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; }
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.
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
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.
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. ?>
Javascript
<script> // javascript program to perform sum of // cubes of first n odd natural numbers function cubesum(n) { return (n) * (n) * (2 * n * n - 1); } // Driver function var a = 4; document.write(cubesum(a)); // This code is contributed by Amit Katiyar </script>
Producción:
496
Análisis de Complejidad:
Complejidad de tiempo: O(1)
Complejidad espacial: O(1)
Este artículo es una contribución de Dharmendra kumar . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA