Find the Nth term of the series 3, 7, 19, 55, 163, . . .
Last Updated :
16 Aug, 2022
Given a positive integer N. The task is to find Nth term of the series 3, 7, 19, 55, 163, …..
Examples:
Input: N = 5
Output: 163
Input: N = 1
Output: 3
Approach: The sequence is formed by using the following pattern. For any value N
TN = 2 * 3N – 1 + 1
Illustration:
Input: N = 5
Output: 163
Explanation:
TN = 2 * 3N – 1 + 1
= 2 * 35 – 1 + 1
= 2 * 81 + 1
= 162 + 1
= 163
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calcNum( int N)
{
return 2 * pow (3, N - 1) + 1;
}
int main()
{
int N = 5;
cout << calcNum(N);
return 0;
}
|
Java
import java.lang.*;
public class gfg
{
static int calcNum( int N)
{
return ( int )( 2 *(Math.pow( 3 ,N- 1 ))) + 1 ;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(calcNum(N));
}
}
|
Python3
def calcNum(N):
return 2 * ( 3 * * (N - 1 )) + 1
N = 5
print (calcNum(N))
|
C#
using System;
public class gfg
{
static int calcNum( int N)
{
return ( int )(2 * (Math.Pow(3, N - 1))) + 1;
}
public static void Main( string [] args)
{
int N = 5;
Console.WriteLine(calcNum(N));
}
}
|
Javascript
<script>
function calcNum(N) {
return 2 * Math.pow(3, N - 1) + 1;
}
let N = 5;
document.write(calcNum(N));
</script>
|
Time Complexity: O(logN) because using inbuilt pow function
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...