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++
// 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!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!