Saturday, June 13, 2026
HomeData Modelling & AINth term of a Custom Fibonacci series

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:
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++




// 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;
}


Java




// 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


Python 3




# 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


C#




// 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.


Javascript




<script>
// javascript implementation of the
// Custom Fibonacci series   
 
// Function to return the nth term
    // of the required sequence
    function nth_term(a , b , n)
    {
        var 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
    var a = 10, b = 17, n = 3;
    document.write(nth_term(a, b, n));
 
// This code is contributed by Rajput-Ji
</script>


Output: 

7

 

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