Sunday, June 14, 2026
HomeData Modelling & AINth XOR Fibonacci number

Nth XOR Fibonacci number

Given three integers a, b and N where a and b are the first two terms of the XOR Fibonacci series and the task is to find the Nth term. 
The Nth term of the XOR Fibonacci series is defined as F(N) = F(N – 1) ^ F(N – 2) where ^ is the bitwise XOR.
Examples: 

Input: a = 1, b = 2, N = 5 
Output:
F(0) = 1 
F(1) = 2 
F(2) = 1 ^ 2 = 3 
F(3) = 2 ^ 3 = 1 
F(4) = 1 ^ 3 = 2 
F(5) = 1 ^ 2 = 3

Input: a = 5, b = 11, N = 1000001 
Output: 14 

 

Approach: Since, a ^ a = 0 and it is given that 
 

F(0) = a and F(1) = b 
Now, F(2) = F(0) ^ F(1) = a ^ b 
And, F(3) = F(1) ^ F(2) = b ^ (a ^ b) = a 
F(4) = a ^ b ^ a = b 
F(5) = a ^ b 
F(6) = a 
F(7) = b 
F(8) = a ^ b 
… 

It can be observed that the answer repeats itself after every 3 numbers. So the answer is F(N % 3) where F(0) = a, F(1) = b and F(2) = a ^ b.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the nth XOR Fibonacci number
int nthXorFib(int n, int a, int b)
{
    if (n == 0)
        return a;
    if (n == 1)
        return b;
    if (n == 2)
        return (a ^ b);
 
    return nthXorFib(n % 3, a, b);
}
 
// Driver code
int main()
{
    int a = 1, b = 2, n = 10;
 
    cout << nthXorFib(n, a, b);
 
    return 0;
}


Java




// Java implementation of the above approach
class GFG
{
         
    // Function to return the
    // nth XOR Fibonacci number
    static int nthXorFib(int n, int a, int b)
    {
        if (n == 0)
            return a;
        if (n == 1)
            return b;
        if (n == 2)
            return (a ^ b);
     
        return nthXorFib(n % 3, a, b);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int a = 1, b = 2, n = 10;
     
        System.out.println(nthXorFib(n, a, b));
    }
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 implementation of the approach
 
# Function to return
# the nth XOR Fibonacci number
def nthXorFib(n, a, b):
    if n == 0 :
        return a
    if n == 1 :
        return b
    if n == 2 :
        return a ^ b
 
    return nthXorFib(n % 3, a, b)
 
# Driver code
a = 1
b = 2
n = 10
print(nthXorFib(n, a, b))
 
# This code is contributed by divyamohan123


C#




// C# implementation of the above approach
using System;
     
class GFG
{
         
    // Function to return the
    // nth XOR Fibonacci number
    static int nthXorFib(int n, int a, int b)
    {
        if (n == 0)
            return a;
        if (n == 1)
            return b;
        if (n == 2)
            return (a ^ b);
     
        return nthXorFib(n % 3, a, b);
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int a = 1, b = 2, n = 10;
     
        Console.WriteLine(nthXorFib(n, a, b));
    }
}
 
// This code is contributed by Princi Singh


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the nth XOR Fibonacci number
function nthXorFib(n, a, b)
{
    if (n == 0)
        return a;
    if (n == 1)
        return b;
    if (n == 2)
        return (a ^ b);
 
    return nthXorFib(n % 3, a, b);
}
 
// Driver code
    let a = 1, b = 2, n = 10;
 
    document.write(nthXorFib(n, a, b));
 
</script>


Output: 

2

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS