Find the Nth term of the series 4, 11, 30, 85, 248. . .
Given a positive integer N. The task is to find Nth term of the series:
4, 11, 30, 85, 248…
Examples:
Input: N = 4
Output: 85
Input: N = 2
Output: 11
Approach:
The Nth term of the given series can be generalized as:
TN = 3N + N
Illustration:
Input: N = 4
Output: 85
Explanation:
TN = 3N +N
= 34 + 4
= 81 +4
= 85
Below is the implementation of the above problem:
C++
#include <bits/stdc++.h>
using namespace std;
int nthTerm( int N)
{
return pow (3, N) + N;
}
int main()
{
int N = 4;
cout << nthTerm(N);
return 0;
}
|
Java
class GFG
{
static int nthTerm( int N) {
return ( int )Math.pow( 3 , N) + N;
}
public static void main(String args[])
{
int N = 4 ;
System.out.println(nthTerm(N));
}
}
|
Python3
def nthTerm(N):
return ( 3 * * N) + N;
N = 4 ;
print (nthTerm(N));
|
C#
using System;
class GFG
{
static int nthTerm( int N) {
return ( int )Math.Pow(3, N) + N;
}
public static void Main()
{
int N = 4;
Console.Write(nthTerm(N));
}
}
|
Javascript
<script>
function nthTerm(N) {
return Math.pow(3, N) + N;
}
let N = 4;
document.write(nthTerm(N));
</script>
|
Time Complexity: O(log3N)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
13 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...