Escriba un programa para encontrar la suma de la cuarta potencia de los primeros n números naturales impares.
1 4 + 3 4 + 5 4 + 7 4 + 9 4 + 11 4 ………….+(2n-1) 4 .
Ejemplos:
Input : 3 Output : 707 14 +34 +54 = 707 Input : 6 Output : 24310 14 + 34 + 54 + 74 + 94 + 114
Enfoque ingenuo: en este simple, encontrar las cuartas potencias de los primeros n números naturales impares es iterar un ciclo de 1 a n veces y almacenar el resultado en una suma variable.
Ej.-n=3 entonces, (1*1*1*1)+(3*3*3*3)+(5*5*5*5) = 707
C++
// CPP Program to find the sum of fourth powers // of first n odd natural numbers #include <bits/stdc++.h> using namespace std; // calculate the sum of fourth power of first // n odd natural numbers long long int oddNumSum(int n) { int j = 0; long long int sum = 0; for (int i = 1; i <= n; i++) { j = (2 * i - 1); sum = sum + (j * j * j * j); } return sum; } // Driven Program int main() { int n = 6; cout << oddNumSum(n) << endl; return 0; }
Java
// Java Program to find the // sum of fourth powers of // first n odd natural numbers import java.io.*; class GFG { // calculate the sum of // fourth power of first // n odd natural numbers static long oddNumSum(int n) { int j = 0; long sum = 0; for (int i = 1; i <= n; i++) { j = (2 * i - 1); sum = sum + (j * j * j * j); } return sum; } // Driven Program public static void main(String args[]) { int n = 6; System.out.println(oddNumSum(n)); } } // This code is contributed // by Nikita tiwari.
Python 3
# Python 3 Program to find the # sum of fourth powers of # first n odd natural numbers # calculate the sum of # fourth power of first # n odd natural numbers def oddNumSum(n) : j = 0 sm = 0 for i in range(1, n + 1) : j = (2 * i - 1) sm = sm + (j * j * j * j) return sm # Driven Program n = 6; print(oddNumSum(n)) # This code is contributed # by Nikita tiwari.
C#
// C# Program to find the // sum of fourth powers of // first n odd natural numbers using System; class GFG { // calculate the sum of // fourth power of first // n odd natural numbers static long oddNumSum(int n) { int j = 0; long sum = 0; for (int i = 1; i <= n; i++) { j = (2 * i - 1); sum = sum + (j * j * j * j); } return sum; } // Driven Program public static void Main() { int n = 6; Console.Write(oddNumSum(n)); } } // This code is contributed by // vt_m.
PHP
<?php // PHP Program to find the // sum of fourth powers // of first n odd natural // numbers // calculate the sum of // fourth power of first // n odd natural numbers function oddNumSum($n) { $j = 0; $sum = 0; for ($i = 1; $i <= $n; $i++) { $j = (2 * $i - 1); $sum = $sum + ($j * $j * $j * $j); } return $sum; } // Driver Code $n = 6; echo(oddNumSum($n)); // This code is contributed by Ajit. ?>
Javascript
<script> // javascript Program to find the sum of fourth powers // of first n odd natural numbers // calculate the sum of fourth power of first // n odd natural numbers function oddNumSum( n) { let j = 0; let sum = 0; for (let i = 1; i <= n; i++) { j = (2 * i - 1); sum = sum + (j * j * j * j); } return sum; } // Driven Program let n = 6; document.write(oddNumSum(n)); // This code contributed by aashish1995 </script>
Producción:
24310
Análisis de Complejidad:
Tiempo Complejidad : O(N)
Complejidad espacial: O (1) ya que no se utiliza espacio adicional
Enfoque eficiente : una solución eficiente es usar una fórmula matemática directa que es:
Cuarta potencia número natural = (1 4 + 2 4 + 3 4 + ………… +n 4 )
= (n(n+1)(2n+1)(3n 2 +3n-1))/30
Cuarta potencia par número natural = (2 4 + 4 4 + 6 4 + ………… +2n 4 )
= 8(n(n+1)(2n+1)(3n 2 +3n-1))/15;
Necesitamos un número natural impar, así que restamos el
(Número natural impar de cuarta potencia) = (Número natural de cuarta potencia primero n) – (Número natural par de cuarta potencia)
= (1 4 + 2 4 + 3 4 + ………… +n 4 ) – (24 + 4 4 + 6 4 + ………… +2n 4 )
= (1 4 + 3 4 + 5 4 + ………… +(2n-1) 4 )
fórmula de unidades
= (2n(2n+1)( 4n+1)(12n 2 +6n-1))/30 – (8(n(n+1)(2n+1)(3n 2 +3n -1)))/15
= 2n(2n+1)/ 30[(4n+1)(12n 2 +6n-1) – ((8n+8)((3n 2 +3n-1))]
= n(2n+1)/15[(48n 3 + 24n 2 – 4n + 12n 2 + 6n -1) – (24n 3 + 24n 2 – 8n + 24n 2 + 24n -8) ]
= n(2n+1)/15[24n 3– 12n 2 – 14n + 7]
Sum of fourth power of first n odd numbers = n(2n+1)/15[24n3 - 12n2 - 14n + 7]
C++
// CPP Program to find the sum of fourth powers // of first n odd natural numbers #include <bits/stdc++.h> using namespace std; // calculate the sum of fourth power of first // n odd natural numbers long long int oddNumSum(int n) { return (n * (2 * n + 1) * (24 * n * n * n - 12 * n * n - 14 * n + 7)) / 15; } // Driven Program int main() { int n = 4; cout << oddNumSum(n) << endl; return 0; }
Java
// Java Program to find the sum of // fourth powers of first n odd // natural numbers class GFG { // calculate the sum of fourth // power of first n odd natural // numbers static long oddNumSum(int n) { return (n * (2 * n + 1) * (24 * n * n * n - 12 * n * n - 14 * n + 7)) / 15; } // Driven Program public static void main(String[] args) { int n = 4; System.out.println(oddNumSum(n)); } } // This code is contributed by // Smitha Dinesh Semwal.
Python 3
# Python 3 Program to find the # sum of fourth powers of first # n odd natural numbers # calculate the sum of fourth # power of first n odd natural #numbers def oddNumSum(n): return (n * (2 * n + 1) * (24 * n * n * n - 12 * n * n - 14 * n + 7)) / 15 # Driven Program n = 4 print(int(oddNumSum(n))) # This code is contributed by # Smitha Dinesh Semwal.
C#
// C# Program to find the sum of // fourth powers of first n // odd natural numbers using System; class GFG { // calculate the sum of fourth // power of first n odd // natural numbers static long oddNumSum(int n) { return (n * (2 * n + 1) * (24 * n * n * n - 12 * n * n - 14 * n + 7)) / 15; } // Driven Program public static void Main() { int n = 4; Console.Write(oddNumSum(n)); } } // This code is contributed by // vt_m.
PHP
<?php // PHP Program to find the // sum of fourth powers // of first n odd natural // numbers // calculate the sum of // fourth power of first // n odd natural numbers function oddNumSum($n) { return ($n * (2 * $n + 1) * (24 * $n * $n * $n - 12 * $n * $n - 14 * $n + 7)) / 15; } // Driver Code $n = 4; echo(oddNumSum($n)); // This code is contributed by Ajit. ?>
Javascript
<script> // javascript Program to find the sum of // fourth powers of first n odd // natural numbers // calculate the sum of fourth // power of first n odd natural // numbers function oddNumSum(n) { return (n * (2 * n + 1) * (24 * n * n * n - 12 * n * n - 14 * n + 7)) / 15; } // Driven Program var n = 4; document.write(oddNumSum(n)); // This code is contributed by Amit Katiyar </script>
Producción:
3108
Complejidad de tiempo : O(1)
Complejidad espacial: 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