Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Find nth term of a given recurrence relation in C++
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
// 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){
// Now get the answer
int ans1 = (n1 * (n1 - 1)) / 2;
//Now return the answer
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
Advertisements
