Find the Nth term of the series 3, 7, 14, 27, 52, . . .
Given a positive integer N. The task is to find Nth term of the series 3, 7, 14, 27, 52, …..
Examples:
Input: N = 5
Output: 52
Input: N = 1
Output: 3
Approach:
The sequence is formed by using the following pattern. For any value N-
TN = (N -1) + 3 * 2N-1
Illustration:
Input: N = 5
Output: 52
Explanation:
TN = (5 – 1) + 3 * 25 – 1
= 4 + 3 * 16
= 52
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calcNum( int N)
{
return ((N - 1) + 3 *
pow (2, N - 1));
}
int main()
{
int N = 5;
cout << calcNum(N);
return 0;
}
|
Java
class GFG
{
static int calcNum( int N) {
return ( int ) ((N - 1 ) + 3 * Math.pow( 2 , N - 1 ));
}
public static void main(String args[]) {
int N = 5 ;
System.out.println(calcNum(N));
}
}
|
Python3
def calcNum(N):
return ((N - 1 ) + 3 * ( 2 * * (N - 1 )));
N = 5 ;
print (calcNum(N));
|
C#
using System;
class GFG {
static int calcNum( int N)
{
return ( int )((N - 1) + 3 * Math.Pow(2, N - 1));
}
public static void Main()
{
int N = 5;
Console.WriteLine(calcNum(N));
}
}
|
Javascript
<script>
function calcNum(N) {
return ((N - 1) + 3 *
Math.pow(2, N - 1));
}
let N = 5;
document.write(calcNum(N));
</script>
|
Time Complexity: O(logn)
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...