Dado un entero positivo N . La tarea es encontrar el término N de la serie:
1, 2, 6, 21, 88, 445, . . .
Ejemplos:
Entrada: N = 3
Salida: 6Entrada: N = 6
Salida: 445
Acercarse:
La secuencia dada sigue el siguiente patrón:
1, (1 * 1 + 1 = 2), (2 * 2 + 2 = 6), (6 * 3 + 3 = 21), (21 * 4 + 4 = 88), (88 * 5 + 5 = 445 ),…
Los pasos a continuación se pueden usar para resolver el problema:
- Para cada iterativo i , multiplicando su elemento anterior por i (Inicialmente el elemento será 1)
- Y suma elementos multiplicados con i.
- Finalmente, devuelva el N-ésimo término de la serie.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program to find N-th term // of the series- // 1, 2, 6, 21, 88, 445... #include <bits/stdc++.h> using namespace std; // Function to return Nth term // of the series int nthTerm(int N) { // Initializing a variable int term = 1; // Loop to iterate from 1 to N-1 for (int i = 1; i < N; i++) { // Multiplying and adding previous // element with i term = term * i + i; } // returning the Nth term return term; } // Driver Code int main() { // Get the value of N int N = 6; cout << nthTerm(N); return 0; }
Java
// Java program to find N-th term // of the series- // 1, 2, 6, 21, 88, 445... class GFG { // Function to return Nth term // of the series static int nthTerm(int N) { // Initializing a variable int term = 1; // Loop to iterate from 1 to N-1 for (int i = 1; i < N; i++) { // Multiplying and adding previous // element with i term = term * i + i; } // returning the Nth term return term; } // Driver Code public static void main(String args[]) { // Get the value of N int N = 6; System.out.println(nthTerm(N)); } } // This code is contributed by saurabh_jaiswal.
Python3
# Python code for the above approach # Function to return Nth term # of the series def nthTerm(N): # Initializing a variable term = 1; # Loop to iterate from 1 to N-1 for i in range(1, N): # Multiplying and adding previous # element with i term = term * i + i; # returning the Nth term return term; # Driver Code # Get the value of N N = 6; print(nthTerm(N)); # This code is contributed by Saurabh Jaiswal
C#
// C# program to find N-th term // of the series- // 1, 2, 6, 21, 88, 445... using System; class GFG { // Function to return Nth term // of the series static int nthTerm(int N) { // Initializing a variable int term = 1; // Loop to iterate from 1 to N-1 for (int i = 1; i < N; i++) { // Multiplying and adding previous // element with i term = term * i + i; } // returning the Nth term return term; } // Driver Code public static void Main() { // Get the value of N int N = 6; Console.Write(nthTerm(N)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach // Function to return Nth term // of the series function nthTerm(N) { // Initializing a variable let term = 1; // Loop to iterate from 1 to N-1 for (let i = 1; i < N; i++) { // Multiplying and adding previous // element with i term = term * i + i; } // returning the Nth term return term; } // Driver Code // Get the value of N let N = 6; document.write(nthTerm(N)); // This code is contributed by Potta Lokesh </script>
Producción
445
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por athakur42u y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA