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