Find nth term of a given recurrence relation - GeeksforGeeks

08 Apr, 2021
Let an be a sequence of numbers, which is defined by the recurrence relation a1=1 and an+1/an=2n. The task is to find the value of log2(an) for a given n.Examples:  
Input: 5
Output: 10
Explanation: 
log2(an) = (n * (n - 1)) / 2
= (5*(5-1))/2
= 10

Input: 100
Output: 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++
Java
Python3
C#
PHP
Javascript
// C++ program to find nth term of// a given recurrence relation #include <bits/stdc++.h>using namespace std; // function to return required valueint sum(int n){     // Get the answer    int ans = (n * (n - 1)) / 2;     // Return the answer    return ans;} // Driver programint main(){     // Get the value of n    int n = 5;     // function call to print result    cout << sum(n);     return 0;}
// Java program to find nth term// of a given recurrence relationimport java.util.*; class solution{static int sum(int n){    // Get the answer    int ans = (n * (n - 1)) / 2;     // Return the answer    return ans;} // Driver codepublic 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
# Python3 program to find nth# term of a given recurrence# relation # function to return# required valuedef sum(n):     # Get the answer    ans = (n * (n - 1)) / 2;         # Return the answer    return ans # Driver Code # Get the value of nn = 5 # function call to prresultprint(int(sum(n))) # This code is contributed by Raj
// C# program to find nth term// of a given recurrence relationusing System; class GFG{static int sum(int n){    // Get the answer    int ans = (n * (n - 1)) / 2;     // Return the answer    return ans;} // Driver codepublic 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
<?php// PHP program to find nth term of// a given recurrence relation // function to return required valuefunction 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 resultecho sum($n); // This code is contributed by// inder_verma?>
<script>// Javascript program to find nth term of// a given recurrence relation // function to return required valuefunction sum(n){     // Get the answer    let ans = parseInt((n * (n - 1)) / 2);     // Return the answer    return ans;} // Driver program // Get the value of nlet n = 5; // function call to print resultdocument.write(sum(n)); // This code is contributed by subham348.</script>
10
 
Time Complexity: O(1) 
SURENDRA_GANGWAR
inderDuMCA
R_Raj
Akanksha_Rai
subham348
Algorithms-Recursion
math
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Program to multiply two matrices
Fizz Buzz Implementation
Complexity Analysis of Binary Search
Modular multiplicative inverse
Check if a number is Palindrome
Find Union and Intersection of two unsorted arrays
Count ways to reach the n'th stair
Find first and last digits of a number