Considere una secuencia a 0 , a 1 , …, a n , donde a i = a i-1 + a i-2 . Dado un 0 , un 1 y un entero positivo n . La tarea es encontrar si una n es par o impar.
Tenga en cuenta que la secuencia dada es como Fibonacci con la diferencia de que los primeros dos términos pueden ser cualquier cosa en lugar de 0 o 1.
Ejemplos:
Input : a0 = 2, a1 = 4, n =3 Output : Even a2 = 6, a3 = 10 And a3 is even. Input : a0 = 1, a1 = 9, n = 2 Output : Even
Método 1: la idea es encontrar la secuencia usando la array y verificar si el elemento n es par o impar.
C++
// CPP Program to check if the nth is odd or even in a // sequence where each term is sum of previous two term #include <bits/stdc++.h> using namespace std; #define MAX 100 // Return if the nth term is even or odd. bool findNature(int a, int b, int n) { int seq[MAX] = { 0 }; seq[0] = a; seq[1] = b; for (int i = 2; i <= n; i++) seq[i] = seq[i - 1] + seq[i - 2]; // Return true if odd return (seq[n] & 1); } // Driven Program int main() { int a = 2, b = 4; int n = 3; (findNature(a, b, n) ? (cout << "Odd" << " ") : (cout << "Even" << " ")); return 0; }
Java
// Java Program to check if // the nth is odd or even // in a sequence where each // term is sum of previous // two term // Return if the nth // term is even or odd. class GFG { public static int findNature(int a, int b, int n) { int[] seq = new int[100]; seq[0] = a; seq[1] = b; for (int i = 2; i <= n; i++) seq[i] = seq[i - 1] + seq[i - 2]; // Return true if odd if((seq[n] & 1) != 0) return 1; else return 0; } // Driver Code public static void main(String[] args) { int a = 2, b = 4; int n = 3; if(findNature(a, b, n) == 1) System.out.println("Odd "); else System.out.println("Even "); } } // This code is contributed // by mits
Python3
# Python3 Program to check if # the nth is odd or even in a # sequence where each term is # sum of previous two term MAX = 100; # Return if the nth # term is even or odd. def findNature(a, b, n): seq = [0] * MAX; seq[0] = a; seq[1] = b; for i in range(2, n + 1): seq[i] = seq[i - 1] + seq[i - 2]; # Return true if odd return (seq[n] & 1); # Driver Code a = 2; b = 4; n = 3; if(findNature(a, b, n)): print("Odd"); else: print("Even"); # This code is contributed by mits
C#
// C# Program to check if the // nth is odd or even in a // sequence where each term // is sum of previous two term using System; // Return if the nth term // is even or odd. class GFG { public static int findNature(int a, int b, int n) { int[] seq = new int[100]; seq[0] = a; seq[1] = b; for (int i = 2; i <= n; i++) seq[i] = seq[i - 1] + seq[i - 2]; // Return true if odd if((seq[n] & 1)!=0) return 1; else return 0; } // Driver Code public static void Main() { int a = 2, b = 4; int n = 3; if(findNature(a, b, n) == 1) Console.Write("Odd "); else Console.Write("Even "); } } // This code is contributed // by mits
PHP
<?php // PHP Program to check if the // nth is odd or even in a // sequence where each term is // sum of previous two term $MAX = 100; // Return if the nth // term is even or odd. function findNature($a, $b, $n) { global $MAX; $seq = array_fill(0,$MAX, 0); $seq[0] = $a; $seq[1] = $b; for ($i = 2; $i <= $n; $i++) $seq[$i] = $seq[$i - 1] + $seq[$i - 2]; // Return true if odd return ($seq[$n] & 1); } // Driver Code $a = 2; $b = 4; $n = 3; if(findNature($a, $b, $n)) echo "Odd"; else echo "Even"; // This code is contributed by mits ?>
Javascript
<script> // Javascript Program to check if the // nth is odd or even in a // sequence where each term is sum // of previous two term var MAX = 100; // Return if the nth term is even or odd. function findNature(a, b, n) { var seq = Array(MAX).fill(0); seq[0] = a; seq[1] = b; for (var i = 2; i <= n; i++) seq[i] = seq[i - 1] + seq[i - 2]; // Return true if odd return (seq[n] & 1); } // Driven Program var a = 2, b = 4; var n = 3; (findNature(a, b, n) ? (document.write( "Odd" + " ")) : (document.write( "Even" + " "))); </script>
Even
Método 2 (eficiente):
observe que la naturaleza (par o impar) del término n depende de los términos anteriores, y la naturaleza del término anterior depende de sus términos anteriores y finalmente depende del valor inicial, es decir, un 0 y un 1 .
Entonces, tenemos cuatro escenarios posibles para un 0 y un 1 :
Caso 1: Cuando un 0 y un 1 son pares
En este caso, cada uno de los valores en la secuencia será solo par.
Caso 2: Cuando un 0 y un 1 son impares
En este caso, observe que un 2 es par, un 3 es impar, un 4 es impar y así sucesivamente. Entonces, podemos decir uni es par si i es de la forma 3*k – 1, de lo contrario impar.
Caso 3: Cuando un 0 es par y un 1 es impar
En este caso, observe que un 2 es impar, un 3 es par, un 4 y un 5 son impares, un 6 es par y así sucesivamente. Entonces, podemos decir, a i es par si i es múltiplo de 3, caso contrario impar
4: Cuando un 0 es impar y un 1 es par
En este caso, observe un 2 y un 3 es impar, un 4 es par, un 5 y un 6 es impar, un 7es par y así sucesivamente. Entonces, podemos decir que a i es par si i es de la forma 3*k + 1, k >= 0, de lo contrario impar.
A continuación se muestra la implementación de este enfoque:
C++
// CPP Program to check if the nth is odd or even in a // sequence where each term is sum of previous two term #include <bits/stdc++.h> using namespace std; // Return if the nth term is even or odd. bool findNature(int a, int b, int n) { if (n == 0) return (a & 1); if (n == 1) return (b & 1); // If a is even if (!(a & 1)) { // If b is even if (!(b & 1)) return false; // If b is odd else return (n % 3 != 0); } // If a is odd else { // If b is odd if (!(b & 1)) return ((n - 1) % 3 != 0); // If b is even else return ((n + 1) % 3 != 0); } } // Driven Program int main() { int a = 2, b = 4; int n = 3; (findNature(a, b, n) ? (cout << "Odd" << " ") : (cout << "Even" << " ")); return 0; }
Java
// Java Program to check if the nth is odd or even in a // sequence where each term is sum of previous two term class GFG{ // Return if the nth term is even or odd. static boolean findNature(int a, int b, int n) { if (n == 0) return (a & 1)==1?true:false; if (n == 1) return (b & 1)==1?true:false; // If a is even if ((a & 1)==0) { // If b is even if ((b & 1)==0) return false; // If b is odd else return (n % 3 != 0); } // If a is odd else { // If b is odd if ((b & 1)==0) return ((n - 1) % 3 != 0); // If b is even else return ((n + 1) % 3 != 0); } } // Driven Program public static void main(String[] args) { int a = 2, b = 4; int n = 3; if(findNature(a, b, n)) System.out.println("Odd"); else System.out.println("Even"); } } // This Code is contributed by mits
Python3
# Python3 Program to check if the # nth is odd or even in a # sequence where each term is # sum of previous two term # Return if the nth # term is even or odd. def findNature(a, b, n): if (n == 0): return (a & 1); if (n == 1): return (b & 1); # If a is even if ((a & 1) == 0): # If b is even if ((b & 1) == 0): return False; # If b is odd else: return True if(n % 3 != 0) else False; # If a is odd else: # If b is odd if ((b & 1) == 0): return True if((n - 1) % 3 != 0) else False; # If b is even else: return True if((n + 1) % 3 != 0) else False; # Driver Code a = 2; b = 4; n = 3; if (findNature(a, b, n) == True): print("Odd", end = " "); else: print("Even", end = " "); # This code is contributed by mits
C#
// C# Program to check if the nth is odd or even in a // sequence where each term is sum of previous two term class GFG{ // Return if the nth term is even or odd. static bool findNature(int a, int b, int n) { if (n == 0) return (a & 1)==1?true:false; if (n == 1) return (b & 1)==1?true:false; // If a is even if ((a & 1)==0) { // If b is even if ((b & 1)==0) return false; // If b is odd else return (n % 3 != 0); } // If a is odd else { // If b is odd if ((b & 1)==0) return ((n - 1) % 3 != 0); // If b is even else return ((n + 1) % 3 != 0); } } // Driven Program static void Main() { int a = 2, b = 4; int n = 3; if(findNature(a, b, n)) System.Console.WriteLine("Odd"); else System.Console.WriteLine("Even"); } } // This Code is contributed by mits
PHP
<?php // PHP Program to check if the // nth is odd or even in a // sequence where each term is // sum of previous two term // Return if the nth // term is even or odd. function findNature($a, $b, $n) { if ($n == 0) return ($a & 1); if ($n == 1) return ($b & 1); // If a is even if (!($a & 1)) { // If b is even if (!($b & 1)) return false; // If b is odd else return ($n % 3 != 0); } // If a is odd else { // If b is odd if (!($b & 1)) return (($n - 1) % 3 != 0); // If b is even else return (($n + 1) % 3 != 0); } } // Driver Code $a = 2; $b = 4; $n = 3; if (findNature($a, $b, $n) == true) echo "Odd"," "; else echo "Even"," "; // This code is contributed by ajit ?>
Javascript
<script> // Javascript Program to check if the nth is odd or even in a // sequence where each term is sum of previous two term // Return if the nth term is even or odd. function findNature(a, b, n) { if (n == 0) return (a & 1)==1?true:false; if (n == 1) return (b & 1)==1?true:false; // If a is even if ((a & 1)==0) { // If b is even if ((b & 1)==0) return false; // If b is odd else return (n % 3 != 0); } // If a is odd else { // If b is odd if ((b & 1)==0) return ((n - 1) % 3 != 0); // If b is even else return ((n + 1) % 3 != 0); } } let a = 2, b = 4; let n = 3; if(findNature(a, b, n)) document.write("Odd"); else document.write("Even"); // This code is contributed by suresh07. </script>
Even