- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find nth term of a given recurrence relation in Python

Suppose we have a sequence of numbers called bn, this is represented using a recurrence relation like b1=1 and bn+1/bn=2n . We have to find the value of log2(bn) for a given n.

So, if the input is like 6, then the output will be 5 as log2(bn) = (n * (n - 1)) / 2 = (6*(6-1))/2 = 15

We can solve this problem by solving this relation as follows −

b_{n+1}/b_{n} = 2^{n}

b_{n}/b_{n-1} = 2^{n-1}

…

b_{2}/b_{1} = 2^{1} , If we multiply all of above, we can get

(b_{n+1}/b_{n}).(b_{n}/b_{n-1})……(b_{2}/b_{1}) = 2^{n + (n-1)+……….+1}

So, b_{n+1}/b_{1} = 2^{n(n+1)/2}

As 1 + 2 + 3 + ………. + (n-1) + n = n(n+1)/2

So, b_{n+1} = 2^{n(n+1)/2} * b_{1}; We are assuming that initial value b_{1} = 1

So, b_{n+1} = 2^{n(n+1)/2}

After substituting (n+1) for n, we get,

b_{n} = 2^{n(n-1)/2}

By taking log both sides, we get,

log_{2}(b_{n}) = n(n-1)/2

## Example

Let us see the following implementation to get better understanding −

def add_upto_n(n): res = (n * (n - 1)) / 2 return res n = 6 print(int(add_upto_n(n)))

## Input

6

## Output

15

- Related Questions & Answers
- Find nth term of a given recurrence relation in C++
- C program to find nth term of given recurrence relation
- Program to find nth Fibonacci term in Python
- Find Pth term of a GP if Mth and Nth terms are given in C++
- Find Nth term (A matrix exponentiation example) in C++
- Find nth term of the Dragon Curve Sequence in C++
- C++ Program to Compute Combinations using Recurrence Relation for nCr
- JavaScript code to find nth term of a series - Arithmetic Progression (AP)
- Program to find nth term in Look and Say Sequence in Python
- Program to find Fibonacci series results up to nth term in Python
- Program to find nth term of a sequence which are divisible by a, b, c in Python
- Program to find Nth term divisible by a or b in C++
- Find the Nth term of the series 9, 45, 243,1377…in C++
- Program to find nth smallest number from a given matrix in Python
- C++ program to find nth term of the series 5, 2, 13 41,...