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.
Input: a = 1, b = 2, N = 5
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
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:
Time Complexity: O(1)
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Check if a M-th fibonacci number divides N-th fibonacci number
- Number of ways to represent a number as sum of k fibonacci numbers
- Finding number of digits in n'th Fibonacci number
- Nth Even Fibonacci Number
- Find the next Non-Fibonacci number
- Find the next fibonacci number
- Fibonacci number in an array
- How to check if a given number is Fibonacci number?
- Count of Fibonacci divisors of a given number
- Check if a number is Full Fibonacci or not
- n'th multiple of a number in Fibonacci Series
- Find the previous fibonacci number
- Program to find Nth odd Fibonacci Number
- Count the nodes whose sum with X is a Fibonacci number
- Python Program for n-th Fibonacci number
- Nth Fibonacci number using Pell's equation
- An efficient way to check whether n-th Fibonacci number is multiple of 10
- Fibonacci Number modulo M and Pisano Period
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.