Dado un entero positivo N , la tarea es encontrar el término N de la serie 0, 1, 1, 2, 5, 29, 841 …
Ejemplos:
Entrada: N = 6
Salida: 29
Explicación: El sexto término de la serie dada es 29.Entrada: N = 1
Salida: 1Entrada: N = 8
Salida: 750797
Enfoque: El problema dado es un problema matemático básico donde A i = A i-1 2 + A i-2 2 . Por lo tanto, cree las variables a = 0 y b = 1 . Itere usando la variable i en el rango [2, N] , y para cada i , calcule el i -ésimo término y actualice el valor de a y b a i – 1 th e i – 2 th término respectivamente.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find N-th term // of the given series int getNthTerm(int N) { if (N < 3) return N - 1; // Initialize Variables repre- // senting 1st and 2nd term long int a = 0, b = 1; // Loop to iterate through the // range [3, N] using variable i for (int i = 3; i <= N; i++) { // pow((i - 2)th term, 2) + // pow((i - 1)th term, 2) long int c = a * a + b * b; // Update a and b a = b; b = c; } // Return Answer return b; } // Driver Code int main() { int N = 8; cout << getNthTerm(N); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find N-th term // of the given series static int getNthTerm(int N) { if (N < 3) return N - 1; // Initialize Variables repre- // senting 1st and 2nd term int a = 0, b = 1; // Loop to iterate through the // range [3, N] using variable i for (int i = 3; i <= N; i++) { // Math.pow((i - 2)th term, 2) + // Math.pow((i - 1)th term, 2) int c = a * a + b * b; // Update a and b a = b; b = c; } // Return Answer return b; } // Driver Code public static void main(String[] args) { int N = 8; System.out.print(getNthTerm(N)); } } // This code is contributed by 29AjayKumar
Python3
# Python code for the above approach # Function to find N-th term # of the given series def getNthTerm(N): if (N < 3): return N - 1 # Initialize Variables repre- # senting 1st and 2nd term a = 0 b = 1 # Loop to iterate through the # range [3, N] using variable i for i in range(3, N + 1): # pow((i - 2)th term, 2) + # pow((i - 1)th term, 2) c = a * a + b * b # Update a and b a = b b = c # Return Answer return b # Driver Code N = 8 print(getNthTerm(N)) # This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach using System; class GFG { // Function to find N-th term // of the given series static int getNthTerm(int N) { if (N < 3) return N - 1; // Initialize Variables repre- // senting 1st and 2nd term long a = 0, b = 1; // Loop to iterate through the // range [3, N] using variable i for (int i = 3; i <= N; i++) { // pow((i - 2)th term, 2) + // pow((i - 1)th term, 2) long c = a * a + b * b; // Update a and b a = b; b = c; } // Return Answer return (int)b; } // Driver Code public static void Main() { int N = 8; Console.Write(getNthTerm(N)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach // Function to find N-th term // of the given series function getNthTerm(N) { if (N < 3) return N - 1; // Initialize Variables repre- // senting 1st and 2nd term let a = 0, b = 1; // Loop to iterate through the // range [3, N] using variable i for (let i = 3; i <= N; i++) { // pow((i - 2)th term, 2) + // pow((i - 1)th term, 2) let c = a * a + b * b; // Update a and b a = b; b = c; } // Return Answer return b; } // Driver Code let N = 8; document.write(getNthTerm(N)); // This code is contributed by Potta Lokesh </script>
750797
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por vansikasharma1329 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA