Nth term of a Custom Fibonacci series

Given three integers A, B and N. A Custom Fibonacci series is defined as F(x) = F(x – 1) + F(x + 1) where F(1) = A and F(2) = B. Now the task is to find the Nth term of this series.

Examples:

Input: A = 10, B = 17, N = 3
Output: 7
10, 17, 7, -10, -17, …

Input: A = 50, B = 12, N = 10
Output: -50

Approach: It can be observed that the series will go on like A, B, B – A, -A, -B, A – B, A, B, B – A, …



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the Custom Fibonacci series
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the nth term
// of the required sequence
int nth_term(int a, int b, int n)
{
    int z = 0;
    if (n % 6 == 1)
        z = a;
    else if (n % 6 == 2)
        z = b;
    else if (n % 6 == 3)
        z = b - a;
    else if (n % 6 == 4)
        z = -a;
    else if (n % 6 == 5)
        z = -b;
    if (n % 6 == 0)
        z = -(b - a);
    return z;
}
  
// Driver code
int main()
{
    int a = 10, b = 17, n = 3;
  
    cout << nth_term(a, b, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the
// Custom Fibonacci series
class GFG
{
  
// Function to return the nth term
// of the required sequence
static int nth_term(int a, int b, int n)
{
    int z = 0;
    if (n % 6 == 1)
        z = a;
    else if (n % 6 == 2)
        z = b;
    else if (n % 6 == 3)
        z = b - a;
    else if (n % 6 == 4)
        z = -a;
    else if (n % 6 == 5)
        z = -b;
    if (n % 6 == 0)
        z = -(b - a);
    return z;
}
  
// Driver code
public static void main(String[] args)
{
    int a = 10, b = 17, n = 3;
  
    System.out.println(nth_term(a, b, n));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the
# Custom Fibonacci series
  
# Function to return the nth term
# of the required sequence
def nth_term(a, b, n):
    z = 0
    if (n % 6 == 1):
        z = a
    elif (n % 6 == 2):
        z = b
    elif (n % 6 == 3):
        z = b - a
    elif (n % 6 == 4):
        z = -a
    elif (n % 6 == 5):
        z = -b
    if (n % 6 == 0):
        z = -(b - a)
    return z
  
# Driver code
if __name__ == '__main__':
    a = 10
    b = 17
    n = 3
  
    print(nth_term(a, b, n))
      
# This code is contributed by Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the
// Custom Fibonacci series
using System;
  
class GFG
{
      
// Function to return the nth term
// of the required sequence
static int nth_term(int a, int b, int n)
{
    int z = 0;
    if (n % 6 == 1)
        z = a;
    else if (n % 6 == 2)
        z = b;
    else if (n % 6 == 3)
        z = b - a;
    else if (n % 6 == 4)
        z = -a;
    else if (n % 6 == 5)
        z = -b;
    if (n % 6 == 0)
        z = -(b - a);
    return z;
}
  
// Driver code
static public void Main ()
{
    int a = 10, b = 17, n = 3;
  
    Console.Write(nth_term(a, b, n));
}
}
  
// This code is contributed by ajit.

chevron_right


Output:

7

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.