Dados tres enteros X , Y y N , la tarea es encontrar el término N de la serie f[i] = f[i – 1] – f[i – 2] , i > 1 donde f[0] = X yf [1] = Y.
Ejemplos:
Entrada: X = 2, Y = 3, N = 3
Salida: -2
La serie será 2 3 1 -2 -3 -1 2 y f[3] = -2
Entrada: X = 3, Y = 7, N = 8
Salida: 4
Enfoque: Una observación importante aquí es que habrá como máximo 6 términos distintos, antes de que la secuencia comience a repetirse. Por lo tanto, encuentre los primeros 6 términos de la serie y luego el N -ésimo término sería el mismo que el (N % 6) -ésimo término.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the Nth term // of the given series int findNthTerm(int x, int y, int n) { int f[6]; // First and second term of the series f[0] = x; f[1] = y; // Find first 6 terms for (int i = 2; i <= 5; i++) f[i] = f[i - 1] - f[i - 2]; // Return the Nth term return f[n % 6]; } // Driver code int main() { int x = 2, y = 3, n = 3; cout << findNthTerm(x, y, n); return 0; }
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to find the nth term of series static int findNthTerm(int x, int y, int n) { int[] f = new int[6]; f[0] = x; f[1] = y; // Loop to add numbers for (int i = 2; i <= 5; i++) f[i] = f[i - 1] - f[i - 2]; return f[n % 6]; } // Driver code public static void main(String args[]) { int x = 2, y = 3, n = 3; System.out.println(findNthTerm(x, y, n)); } } // This code is contributed by mohit kumar 29
Python3
# Python3 implementation of the approach # Function to return the Nth term # of the given series def findNthTerm(x, y, n): f = [0] * 6 # First and second term of # the series f[0] = x f[1] = y # Find first 6 terms for i in range(2, 6): f[i] = f[i - 1] - f[i - 2] # Return the Nth term return f[n % 6] # Driver code if __name__ == "__main__": x, y, n = 2, 3, 3 print(findNthTerm(x, y, n)) # This code is contributed by # Rituraj Jain
C#
// C# implementation of the approach using System; class GFG { // Function to find the nth term of series static int findNthTerm(int x, int y, int n) { int[] f = new int[6]; f[0] = x; f[1] = y; // Loop to add numbers for (int i = 2; i <= 5; i++) f[i] = f[i - 1] - f[i - 2]; return f[n % 6]; } // Driver code public static void Main() { int x = 2, y = 3, n = 3; Console.WriteLine(findNthTerm(x, y, n)); } } // This code is contributed by Ryuga
PHP
<?php //PHP implementation of the approach // Function to find the nth term of series function findNthTerm($x, $y, $n) { $f = array(6); $f[0] = $x; $f[1] = $y; // Loop to add numbers for ($i = 2; $i <= 5; $i++) $f[$i] = $f[$i - 1] - $f[$i - 2]; return $f[$n % 6]; } // Driver code $x = 2; $y = 3; $n = 3; echo(findNthTerm($x, $y, $n)); // This code is contributed by Code_Mech. ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the Nth term // of the given series function findNthTerm(x, y, n) { let f = new Array(6); // First and second term of the series f[0] = x; f[1] = y; // Find first 6 terms for (let i = 2; i <= 5; i++) f[i] = f[i - 1] - f[i - 2]; // Return the Nth term return f[n % 6]; } // Driver code let x = 2, y = 3, n = 3; document.write(findNthTerm(x, y, n)); // This code is contributed by Surbhi Tyagi </script>
-2
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA