Given a number N, the task is to find the Nth term of the series 2, 15, 41, 80, 132….
Input: N = 2
Input: N = 5
Approach: From the given series, the formula for Nth term can be found as:
1st term = 2 2nd term = 2 + 1 * 13 = 15 3rd term = 15 + 2 * 13 = 41 4th term = 41 + 3 * 13 = 80 . . Nth term = (N - 1)th term + (N - 1) * 13
Therefore, the Nth term of the series is given as
Below are the steps to find the Nth term using recursion:
Recursively iterate from value N:
- Base case: If the value called recursively is 1, then it is the first term of the series. Therefore return 2 from the function.
if(N == 1) return 2;
- Recursive call: If the base case is not met, then recursively iterate from the function according to the Nth term of the series:
(N - 1) * 13 + func(N - 1);
- Return statement: At each recursive call(except the base case), return the recursive function for next iteration.
return ((13 * (N - 1)) + func(N, i + 1));
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.