# Find nth term of a given recurrence relation in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

## Concept

Assume bn be a sequence of numbers, which is denoted by the recurrence relation b1=1 and bn+1/bn=2n. Our task is to determine the value of log2(bn) for a given n.

## Input

6

## Output

15

## Explanation

log2(bn) = (n * (n - 1)) / 2 = (6*(6-1))/2 = 15

## Input

200

## Output

19900

## Method

bn+1/bn = 2n

bn/bn-1 = 2n-1

.

.

.

b2/b1 = 21, We multiply all of above in order to attain

(bn+1/bn).(bn/n-1)……(b2/b1) = 2n + (n-1)+……….+1

So, bn+1/b1 = 2n(n+1)/2

Because we know, 1 + 2 + 3 + ………. + (n-1) + n = n(n+1)/2

So, bn+1 = 2n(n+1)/2 . b1; Assume the initial value b1 = 1

So, bn+1 = 2sup>n(n+1)/2

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

bn = 2n(n-1)/2

Taking log both sides, we get,

log2(bn) = n(n-1)/2

## Example

Live Demo

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

## Output

19900
Updated on 25-Jul-2020 10:45:38