# Find nth term of a given recurrence relation

Let **a _{n}** be a sequence of numbers, which is defined by the recurrence relation

**a**and

_{1}=1**a**. The task is to find the value of

_{n+1}/a_{n}=2^{n}**log**for a given

_{2}(a_{n})**n**.

**Examples:**

Input:5Output:10Explanation:log_{2}(a_{n}) = (n * (n - 1)) / 2 = (5*(5-1))/2 = 10Input:100Output:4950

, We multiply all of the above in order to reach

Since . Then

.

Substituting n+1 for n:

So,

Below is the implementation of above approach.

## C++

`// C++ program to find nth term of ` `// a given recurrence relation ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to return required value ` `int` `sum(` `int` `n) ` `{ ` ` ` ` ` `// Get the answer ` ` ` `int` `ans = (n * (n - 1)) / 2; ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` ` ` `// Get the value of n ` ` ` `int` `n = 5; ` ` ` ` ` `// function call to print result ` ` ` `cout << sum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find nth term ` `// of a given recurrence relation ` `import` `java.util.*; ` ` ` `class` `solution ` `{ ` `static` `int` `sum(` `int` `n) ` `{ ` ` ` `// Get the answer ` ` ` `int` `ans = (n * (n - ` `1` `)) / ` `2` `; ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String arr[]) ` `{ ` ` ` ` ` `// Get the value of n ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `// function call to print result ` ` ` `System.out.println(sum(n)); ` `} ` `} ` `//This code is contributed byte ` `//Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find nth ` `# term of a given recurrence ` `# relation ` ` ` `# function to return ` `# required value ` `def` `sum` `(n): ` ` ` ` ` `# Get the answer ` ` ` `ans ` `=` `(n ` `*` `(n ` `-` `1` `)) ` `/` `2` `; ` ` ` ` ` `# Return the answer ` ` ` `return` `ans ` ` ` `# Driver Code ` ` ` `# Get the value of n ` `n ` `=` `5` ` ` `# function call to prresult ` `print` `(` `int` `(` `sum` `(n))) ` ` ` `# This code is contributed by Raj ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find nth term ` `// of a given recurrence relation ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `sum(` `int` `n) ` `{ ` ` ` `// Get the answer ` ` ` `int` `ans = (n * (n - 1)) / 2; ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` ` ` `// Get the value of n ` ` ` `int` `n = 5; ` ` ` ` ` `// function call to print result ` ` ` `Console.WriteLine(sum(n)); ` `} ` `} ` ` ` `// This code is contributed byte ` `// inder_verma ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find nth term of ` `// a given recurrence relation ` ` ` `// function to return required value ` `function` `sum(` `$n` `) ` `{ ` ` ` ` ` `// Get the answer ` ` ` `$ans` `= (` `$n` `* (` `$n` `- 1)) / 2; ` ` ` ` ` `// Return the answer ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver Code ` ` ` `// Get the value of n ` `$n` `= 5; ` ` ` `// function call to print result ` `echo` `sum(` `$n` `); ` ` ` `// This code is contributed by ` `// inder_verma ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10

**Time Complexity: O(1)**

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.

## Recommended Posts:

- Find Nth term of the series where each term differs by 6 and 2 alternately
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find Pth term of a GP if Mth and Nth terms are given
- Find Nth term of the series 1, 8, 54, 384...
- Find the Nth term of the series 2, 15, 41, 80, 132...
- Find the nth term of the given series
- Find Nth term of the series 1, 1, 2, 6, 24...
- Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ...
- Find nth term of the series 5 2 13 41
- Find the Nth term of the series 14, 28, 20, 40,.....
- Find the Nth term in series 12, 35, 81, 173, 357, ...
- Find Nth term of the series 4, 2, 2, 3, 6, ...
- Find Nth term of the series 2, 3, 10, 15, 26....
- Find Nth term of the series 5, 13, 25, 41, 61...
- Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 ...
- Find Nth term of the series 3, 14, 39, 84...
- Find the Nth term divisible by a or b or c
- Find Nth term of the series 1, 5, 32, 288 ...
- Find Nth term of the series 1, 6, 18, 40, 75, ....

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.