 Open in App
Not now

# Sum of the first N terms of XOR Fibonacci series

• Last Updated : 13 Jan, 2022

Given three positive integers A, B, and N where A and B are the first two terms of the XOR Fibonacci series, the task is to find the sum of the first N terms of XOR Fibonacci series which is defined as follows:

F(N) = F(N – 1) ^ F(N – 2)

where ^ is the bitwise XOR and F(0) is 1 and F(1) is 2.

Examples:

Input: A = 0, B = 1, N = 3
Output: 2
Explanation: The first 3 terms of the XOR Fibonacci Series are 0, 1, 1. The sum of the series of the first 3 terms = 0 + 1 + 1 = 2.

Input: a = 2, b = 5, N = 8
Output: 35

Naive Approach: The simplest approach is to generate the XOR Fibonacci series up to the first N terms and calculate the sum. Finally, print the sum of obtained.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to calculate the sum of the``// first N terms of XOR Fibonacci Series``void` `findSum(``int` `a, ``int` `b, ``int` `n)``{``    ``// Base Case``    ``if` `(n == 1) {``        ``cout << a;``        ``return``;``    ``}` `    ``// Stores the sum of``    ``// the first N terms``    ``int` `s = a + b;` `    ``// Iterate from [0, n-3]``    ``for` `(``int` `i = 0; i < n - 2; i++) {` `        ``// Store XOR of last 2 elements``        ``int` `x = a xor b;` `        ``// Update sum``        ``s += x;` `        ``// Update the first element``        ``a = b;` `        ``// Update the second element``        ``b = x;``    ``}` `    ``// Print the final sum``    ``cout << s;``}` `// Driver Code``int` `main()``{``    ``int` `a = 2, b = 5, N = 8;` `    ``// Function Call``    ``findSum(a, b, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{``    ` `// Function to calculate the sum of the``// first N terms of XOR Fibonacci Series``static` `void` `findSum(``int` `a, ``int` `b, ``int` `n)``{``    ` `    ``// Base Case``    ``if` `(n == ``1``)``    ``{``        ``System.out.println(a);``        ``return``;``    ``}``    ` `    ``// Stores the sum of``    ``// the first N terms``    ``int` `s = a + b;``    ` `    ``// Iterate from [0, n-3]``    ``for``(``int` `i = ``0``; i < n - ``2``; i++)``    ``{``        ` `        ``// Store XOR of last 2 elements``        ``int` `x = a ^ b;``        ` `        ``// Update sum``        ``s += x;``        ` `        ``// Update the first element``        ``a = b;``        ` `        ``// Update the second element``        ``b = x;``    ``}``    ` `    ``// Print the final sum``    ``System.out.println(s);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `a = ``2``, b = ``5``, N = ``8``;``    ` `    ``// Function Call``    ``findSum(a, b, N);``}``}` `// This code is contributed by jana_sayantan`

## Python3

 `# Python3 program for the above approach` `# Function to calculate the sum of the``# first N terms of XOR Fibonacci Series``def` `findSum(a, b, N):``    ` `    ``# Base case``    ``if` `N ``=``=` `1``:``        ``print``(a)``        ``return``    ` `    ``# Stores the sum of``    ``# the first N terms``    ``s ``=` `a ``+` `b` `    ``# Iterate from [0, n-3]``    ``for` `i ``in` `range``(``0``, N ``-` `2``):``        ` `        ``# Store XOR of last 2 elements``        ``x ``=` `a ^ b``        ` `        ``# Update sum``        ``s ``+``=` `x``        ` `        ``# Update the first element``        ``a ``=` `b``        ` `        ``# Update the second element``        ``b ``=` `x``        ` `    ``# Print the final sum``    ``print``(s)``    ``return` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``a ``=` `2``    ``b ``=` `5``    ``N ``=` `8``    ` `    ``# Function call``    ``findSum(a, b, N)` `# This code is contributed by MuskanKalra1`

## C#

 `// C# program for the above approach ``using` `System;``class` `GFG``{``     ` `// Function to calculate the sum of the``// first N terms of XOR Fibonacci Series``static` `void` `findSum(``int` `a, ``int` `b, ``int` `n)``{``     ` `    ``// Base Case``    ``if` `(n == 1)``    ``{``        ``Console.WriteLine(a);``        ``return``;``    ``}``     ` `    ``// Stores the sum of``    ``// the first N terms``    ``int` `s = a + b;``     ` `    ``// Iterate from [0, n-3]``    ``for``(``int` `i = 0; i < n - 2; i++)``    ``{``         ` `        ``// Store XOR of last 2 elements``        ``int` `x = a ^ b;``         ` `        ``// Update sum``        ``s += x;``         ` `        ``// Update the first element``        ``a = b;``         ` `        ``// Update the second element``        ``b = x;``    ``}``     ` `    ``// Print the final sum``    ``Console.WriteLine(s);``}`` ` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `a = 2, b = 5, N = 8;``     ` `    ``// Function Call``    ``findSum(a, b, N);``}``}` `// This code is contributed by susmitakundugoaldanga`

## Javascript

 ``

Output:

`35`

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

Efficient Approach: To optimize the above approach, the idea is based on the following observation:

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 series repeats itself after every 3 numbers. So, the following three cases arises:

1. If N is divisible by 3: Sum of the series is (N / 3) * (a + b + x), where x is XOR of a and b.
2. If N % 3 leaves a remainder 1: Sum of the series is (N / 3)*(a + b + x) + a, where x is the Bitwise XOR of a and b.
3. If N % 3 leaves a remainder 2: Sum of the series is (N / 3)*(a + b + x) + a + b, where x is the Bitwise XOR of a and b.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to calculate sum of the``// first N terms of XOR Fibonacci Series``void` `findSum(``int` `a, ``int` `b, ``int` `n)``{``    ``// Store the sum of first n terms``    ``int` `sum = 0;` `    ``// Store XOR of a and b``    ``int` `x = a ^ b;` `    ``// Case 1: If n is divisible by 3``    ``if` `(n % 3 == 0) {``        ``sum = (n / 3) * (a + b + x);``    ``}` `    ``// Case 2: If n % 3 leaves remainder 1``    ``else` `if` `(n % 3 == 1) {``        ``sum = (n / 3) * (a + b + x) + a;``    ``}` `    ``// Case 3: If n % 3 leaves remainder 2``    ``// on division by 3``    ``else` `{``        ``sum = (n / 3) * (a + b + x) + a + b;``    ``}` `    ``// Print the final sum``    ``cout << sum;``}` `// Driver Code``int` `main()``{``    ``int` `a = 2, b = 5, N = 8;` `    ``// Function Call``    ``findSum(a, b, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``  ` `class` `GFG{` `// Function to calculate sum of the``// first N terms of XOR Fibonacci Series``static` `void` `findSum(``int` `a, ``int` `b, ``int` `n)``{``    ` `    ``// Store the sum of first n terms``    ``int` `sum = ``0``;` `    ``// Store XOR of a and b``    ``int` `x = a ^ b;` `    ``// Case 1: If n is divisible by 3``    ``if` `(n % ``3` `== ``0``)``    ``{``        ``sum = (n / ``3``) * (a + b + x);``    ``}` `    ``// Case 2: If n % 3 leaves remainder 1``    ``else` `if` `(n % ``3` `== ``1``)``    ``{``        ``sum = (n / ``3``) * (a + b + x) + a;``    ``}` `    ``// Case 3: If n % 3 leaves remainder 2``    ``// on division by 3``    ``else``    ``{``        ``sum = (n / ``3``) * (a + b + x) + a + b;``    ``}` `    ``// Print the final sum``    ``System.out.print(sum);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `a = ``2``, b = ``5``, N = ``8``;``    ` `    ``// Function Call``    ``findSum(a, b, N);``}``}` `// This code is contributed by shivanisinghss2110`

## Python3

 `# Python3 program for the above approach` `# Function to calculate sum of the``# first N terms of XOR Fibonacci Series``def` `findSum(a, b, N):``    ` `    ``# Store the sum of first n terms``    ``sum` `=` `0``    ` `    ``# Store xor of a and b``    ``x ``=` `a ^ b``    ` `    ``# Case 1: If n is divisible by 3``    ``if` `N ``%` `3` `=``=` `0``:``        ``sum` `=` `(N ``/``/` `3``) ``*` `(a ``+` `b ``+` `x)``        ` `    ``# Case 2: If n % 3 leaves remainder 1``    ``elif` `N ``%` `3` `=``=` `1``:``        ``sum` `=` `(N ``/``/` `3``) ``*` `(a ``+` `b ``+` `x) ``+` `a``        ` `    ``# Case 3: If n % 3 leaves remainder 2``    ``# on division by 3``    ``else``:``        ``sum` `=` `(N ``/``/` `3``) ``*` `(a ``+` `b ``+` `x) ``+` `a ``+` `b``        ` `    ``# Print the final sum``    ``print``(``sum``)``    ``return` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``a ``=` `2``    ``b ``=` `5``    ``N ``=` `8``    ` `    ``# Function call``    ``findSum(a, b, N)` `# This code is contributed by MuskanKalra1`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to calculate sum of the``// first N terms of XOR Fibonacci Series``static` `void` `findSum(``int` `a, ``int` `b, ``int` `n)``{``    ` `    ``// Store the sum of first n terms``    ``int` `sum = 0;` `    ``// Store XOR of a and b``    ``int` `x = a ^ b;` `    ``// Case 1: If n is divisible by 3``    ``if` `(n % 3 == 0)``    ``{``        ``sum = (n / 3) * (a + b + x);``    ``}` `    ``// Case 2: If n % 3 leaves remainder 1``    ``else` `if` `(n % 3 == 1)``    ``{``        ``sum = (n / 3) * (a + b + x) + a;``    ``}` `    ``// Case 3: If n % 3 leaves remainder 2``    ``// on division by 3``    ``else``    ``{``        ``sum = (n / 3) * (a + b + x) + a + b;``    ``}` `    ``// Print the final sum``    ``Console.Write(sum);``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `a = 2, b = 5, N = 8;``    ` `    ``// Function Call``    ``findSum(a, b, N);``}``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``

Output

`35`

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

My Personal Notes arrow_drop_up