Dada una serie 1, 17, 98, 354 …… Encuentra el término n de esta serie.
La serie básicamente representa la suma de la cuarta potencia de los primeros n números naturales. El primer término es la suma de 1 4 . El segundo término es la suma de dos números, es decir (1 4 + 2 4 = 17), el tercer término es decir (1 4 + 2 4 + 3 4 = 98) y así sucesivamente.
Ejemplos:
Input : 5 Output : 979 Input : 7 Output : 4676
Enfoque ingenuo:
una solución simple es sumar las cuartas potencias de los primeros n números naturales. Al usar la iteración, podemos encontrar fácilmente el término n de la serie.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to find n-th term of // series #include <iostream> using namespace std; // Function to find the nth term of series int sumOfSeries(int n) { // Loop to add 4th powers int ans = 0; for (int i = 1; i <= n; i++) ans += i * i * i * i; return ans; } // Driver code int main() { int n = 4; cout << sumOfSeries(n); return 0; }
Java
// Java program to find n-th term of // series import java.io.*; class GFG { // Function to find the nth term of series static int sumOfSeries(int n) { // Loop to add 4th powers int ans = 0; for (int i = 1; i <= n; i++) ans += i * i * i * i; return ans; } // Driver code public static void main(String args[]) { int n = 4; System.out.println(sumOfSeries(n)); } }
Python3
# Python 3 program to find # n-th term of # series # Function to find the # nth term of series def sumOfSeries(n) : # Loop to add 4th powers ans = 0 for i in range(1, n + 1) : ans = ans + i * i * i * i return ans # Driver code n = 4 print(sumOfSeries(n))
C#
// C# program to find n-th term of // series using System; class GFG { // Function to find the // nth term of series static int sumOfSeries(int n) { // Loop to add 4th powers int ans = 0; for (int i = 1; i <= n; i++) ans += i * i * i * i; return ans; } // Driver code public static void Main() { int n = 4; Console.WriteLine(sumOfSeries(n)); } } // This code is contributed by anuj_67
PHP
<?php // PHP program to find // n-th term of series // Function to find the // nth term of series function sumOfSeries( $n) { // Loop to add 4th powers $ans = 0; for ( $i = 1; $i <= $n; $i++) $ans += $i * $i * $i * $i; return $ans; } // Driver code $n = 4; echo sumOfSeries($n); // This code is contributed // by anuj_67 ?>
Javascript
<script> // Javascript program to find n-th term of // series // Function to find the nth term of series function sumOfSeries( n) { // Loop to add 4th powers let ans = 0; for (let i = 1; i <= n; i++) ans += i * i * i * i; return ans; } // Driver Code let n = 4; document.write(sumOfSeries(n)); </script>
354
Complejidad temporal: O(n).
Enfoque eficiente:
el patrón en esta serie es que el término n es igual a la suma del término (n-1) y n 4 .
Ejemplos:
n = 2 2nd term equals to sum of 1st term and 24 i.e 16 A2 = A1 + 16 = 1 + 16 = 17 Similarly, A3 = A2 + 34 = 17 + 81 = 98 and so on..
Obtenemos:
A(n) = A(n - 1) + n4 = A(n - 2) + n4 + (n-1)4 = A(n - 3) + n4 + (n-1)4 + (n-2)4 . . . = A(1) + 16 + 81... + (n-1)4 + n4 A(n) = 1 + 16 + 81 +... + (n-1)4 + n4 = n(n + 1)(6n3 + 9n2 + n - 1) / 30 i.e A(n) is sum of 4th powers of First n natural numbers.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to find the n-th // term in series #include <bits/stdc++.h> using namespace std; // Function to find nth term int sumOfSeries(int n) { return n * (n + 1) * (6 * n * n * n + 9 * n * n + n - 1) / 30; } // Driver code int main() { int n = 4; cout << sumOfSeries(n); return 0; }
Java
// Java program to find the n-th // term in series import java.io.*; class Series { // Function to find nth term static int sumOfSeries(int n) { return n * (n + 1) * (6 * n * n * n + 9 * n * n + n - 1) / 30; } // Driver Code public static void main(String[] args) { int n = 4; System.out.println(sumOfSeries(n)); } }
Python
# Python program to find the Nth # term in series # Function to print nth term # of series def sumOfSeries(n): return n * (n + 1) * (6 * n * n * n + 9 * n * n + n - 1)/ 30 # Driver code n = 4 print sumOfSeries(n)
C#
// C# program to find the n-th // term in series using System; class Series { // Function to find nth term static int sumOfSeries(int n) { return n * (n + 1) * (6 * n * n * n + 9 * n * n + n - 1) / 30; } // Driver Code public static void Main() { int n = 4; Console.WriteLine(sumOfSeries(n)); } } // This code is contributed by anuj_67
PHP
<?php // PHP program to find the n-th // term in series // Function to find nth term function sumOfSeries( $n) { return $n * ($n + 1) * (6 * $n * $n * $n + 9 * $n * $n + $n - 1) / 30; } // Driver code $n = 4; echo sumOfSeries($n); // This code is contributed by anuj_67 ?>
Javascript
<script> // Javascript program to find the n-th term in series // Function to find nth term function sumOfSeries(n) { return n * (n + 1) * (6 * n * n * n + 9 * n * n + n - 1) / 30; } let n = 4; document.write(sumOfSeries(n)); // This code is contributed by divyeshrabadiya07. </script>
354
Complejidad temporal: O(1).
Publicación traducida automáticamente
Artículo escrito por Prasad_Kshirsagar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA