Check if the n-th term is odd or even in a Fibonacci like sequence
Consider a sequence a0, a1, …, an, where ai = ai-1 + ai-2. Given a0, a1, and a positive integer n. The task is to find whether an is odd or even.
Note that the given sequence is like Fibonacci with the difference that the first two terms can be anything instead of 0 or 1.
Examples :
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
Method 1: The idea is to find the sequence using the array and check if nth element is even or odd.
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
Method 2 (efficient) :
Observe, the nature (odd or even) of the nth term depends on the previous terms, and the nature of the previous term depends on their previous terms and finally depends on the initial value i.e a0 and a1.
So, we have four possible scenarios for a0 and a1:
Case 1: When a0 an a1 is even
In this case each of the value in the sequence will be even only.
Case 2: When a0 an a1 is odd
In this case, observe a2 is even, a3 is odd, a4 is odd and so on. So, we can say ai is even if i is of form 3*k – 1, else odd.
Case 3: When a0 is even and a1 is odd
In this case, observe a2 is odd, a3 is even, a4 and a5 is odd, a6 is even and so on. So, we can say, ai is even if i is multiple of 3, else odd
Case 4: When a0 is odd and a1 is even
In this case, observe a2 and a3 is odd, a4 is even, a5 and a6 is odd, a7 is even and so on. So, we can say, ai is even if i is of the form 3*k + 1, k >= 0, else odd.
Below is the implementation of this approach:
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
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...