Escriba un programa para encontrar la suma de la cuarta potencia de los primeros n números naturales pares.
2 4 + 4 4 + 6 4 + 8 4 + 10 4 +………+2n 4
Ejemplos:
Input : 3 Output : 1568 24 +44 +64 = 1568 Input : 6 Output : 36400 24 + 44 + 64 + 84 + 104 + 124
Enfoque ingenuo : en este simple, encontrar las cuartas potencias de los primeros n números naturales pares es iterar un ciclo de 1 a n veces. Cada i-ésima iteración se almacena en variable y continúa hasta (i!=n). Esto es tomar una Complejidad de Tiempo O(N).
C++
// CPP Program to find the sum of fourth powers of // first n even natural numbers #include <bits/stdc++.h> using namespace std; // calculate the sum of fourth power of first n even // natural numbers long long int evenPowerSum(int n) { long long int sum = 0; for (int i = 1; i <= n; i++) { // made even number int j = 2 * i; sum = sum + (j * j * j * j); } return sum; } // Driven Program int main() { int n = 5; cout << evenPowerSum(n) << endl; return 0; }
Java
// Java Program to find the sum of fourth powers of // first n even natural numbers import java.io.*; class GFG { // calculate the sum of fourth power of first // n even natural numbers static long evenPowerSum(int n) { long sum = 0; for (int i = 1; i <= n; i++) { // made even number int j = 2 * i; sum = sum + (j * j * j * j); } return sum; } // Driven Program public static void main (String[] args) { int n = 5; System.out.println(evenPowerSum(n)); } } /*This code is contributed by vt_m.*/
Python3
# Python3 Program to find # the sum of fourth powers of # first n even natural numbers # calculate the sum of fourth # power of first n even # natural numbers def evenPowerSum(n): sum = 0; for i in range(1, n + 1): # made even number j = 2 * i; sum = sum + (j * j * j * j); return sum; # Driver Code n = 5; print(evenPowerSum(n)); # This is contributed by mits.
C#
// C# Program to find the sum of fourth powers of // first n even natural numbers using System; class GFG { // calculate the sum of fourth power of // first n even natural numbers static long evenPowerSum(int n) { long sum = 0; for (int i = 1; i <= n; i++) { // made even number int j = 2 * i; sum = sum + (j * j * j * j); } return sum; } // Driven Program public static void Main() { int n = 5; Console.Write(evenPowerSum(n)); } } // This code is contributed by vt_m.
PHP
<?php // PHP Program to find the // sum of fourth powers of // first n even natural numbers // calculate the sum of // fourth power of first // n even natural numbers function evenPowerSum($n) { $sum = 0; for ($i = 1; $i <= $n; $i++) { // made even number $j = 2 * $i; $sum = $sum + ($j * $j * $j * $j); } return $sum; } // Driver Code $n = 5; echo(evenPowerSum($n)); // This code is contributed by Ajit. ?>
Javascript
<script> // JavaScript Program to find the sum of fourth powers of // first n even natural numbers // calculate the sum of fourth power of first n even // natural numbers function evenPowerSum( n) { let sum = 0; for (let i = 1; i <= n; i++) { // made even number let j = 2 * i; sum = sum + (j * j * j * j); } return sum; } // Driven Program let n = 5; document.write(evenPowerSum(n)); // This code is contributed by Rajput-Ji </script>
Producción:
15664
Complejidad de tiempo: O (n)
Enfoque eficiente: – Una solución eficiente es usar la fórmula matemática directa que se deriva a continuación, esto es tomar solo O (1) Complejidad de tiempo.
Suma de la cuarta potencia del primer n número natural par = 8*(n*(n+1)*(2*n+1)(3*n 2 +3*n -1))/15
¿Cómo funciona esta fórmula?
La suma de la cuarta potencia de los números naturales es = (n(n+1)(2n+1)(3n 2 +3n-1))/30
necesitamos un número natural par por lo que multiplicamos cada término 2 4
= 2 4 (1 4 + 2 4 + 3 4 + ………… +n 4 )
= (2 4 + 4 4 + 6 4 + ………… +2n 4 )
= 2 4 *(suma del número natural a la cuarta potencia)
= 16*( n*(n+1)*(2*n+1)(3*n 2+3*n -1))/30
= 8*(n*(n+1)*(2*n+1)(3*n 2 +3*n -1))/15
C++
// CPP Program to find the sum of fourth powers of // first n even natural numbers #include <bits/stdc++.h> using namespace std; // calculate the sum of fourth power of first n // even natural numbers long long int evenPowerSum(int n) { return (8 * n * (n + 1) * (2 * n + 1) * (3 * n * n + 3 * n - 1)) / 15; } // Driven Program int main() { int n = 4; cout << evenPowerSum(n) << endl; return 0; }
Java
// JAVA Program to find the sum of fourth powers of // first n even natural numbers import java.io.*; class GFG { // calculate the sum of fourth power of first n // even natural numbers static long evenPowerSum(int n) { return (8 * n * (n + 1) * (2 * n + 1) * (3 * n * n + 3 * n - 1)) / 15; } // Driven Program public static void main (String[] args) { int n = 4; System.out.println(evenPowerSum(n)); } } /* This code is contributed by vt_m. */
Python3
# Python3 Program to find # the sum of fourth powers # of first n even natural # numbers # calculate the sum of # fourth power of first n # even natural numbers def evenPowerSum(n): return (8 * n * (n + 1) * (2 * n + 1) * (3 * n * n + 3 * n - 1)) / 15; # Driver Code n = 4; print (int(evenPowerSum(n))); # This code is contributed by mits
C#
// C# Program to find the sum of fourth powers of // first n even natural numbers using System; class GFG { // calculate the sum of fourth power of first n // even natural numbers static long evenPowerSum(int n) { return (8 * n * (n + 1) * (2 * n + 1) * (3 * n * n + 3 * n - 1)) / 15; } // Driven Program public static void Main() { int n = 4; Console.Write(evenPowerSum(n)); } } /* This code is contributed by vt_m.*/
PHP
<?php // PHP Program to find the // sum of fourth powers of // first n even natural numbers // calculate the sum of // fourth power of first n // even natural numbers function evenPowerSum($n) { return (8 * $n * ($n + 1) * (2 * $n + 1) * (3 * $n * $n + 3 * $n - 1)) / 15; } // Driver Code $n = 4; echo(evenPowerSum($n)); // This code is contributed by Ajit. ?>
Javascript
<script> // Javascript Program to find the sum of fourth powers of // first n even natural numbers // calculate the sum of fourth power of first n // even natural numbers function evenPowerSum(n) { return (8 * n * (n + 1) * (2 * n + 1) * (3 * n * n + 3 * n - 1)) / 15; } // Driven Program var n = 4; document.write(evenPowerSum(n)); // This code is contributed by Rajput-Ji </script>
Producción:
5664
Complejidad de tiempo : O(1)
Publicación traducida automáticamente
Artículo escrito por jaingyayak y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA