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: 31Input: 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++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std;
// Function to return // Nth term of the series int findTerm( int N)
{ return pow (2, N) - 1;
} // Driver Code int main()
{ int N = 5;
cout << findTerm(N);
return 0;
} |
Java
// Java program to implement // the above approach import java.util.*;
public class GFG
{ // Function to return
// Nth term of the series
static int findTerm( int N)
{
return ( int )Math.pow( 2 , N) - 1 ;
}
// Driver Code
public static void main(String args[])
{
int N = 5 ;
System.out.println(findTerm(N));
}
} // This code is contributed by Samim Hossain Mondal. |
Python
# Python program to implement # the above approach # Function to return # Nth term of the series def findTerm(N):
return pow ( 2 , N) - 1
# Driver Code N = 5
print (findTerm(N))
# This code is contributed by samim2000. |
C#
// C# program to implement // the above approach using System;
class GFG
{ // Function to return
// Nth term of the series
static int findTerm( int N)
{
return ( int )Math.Pow(2, N) - 1;
}
// Driver Code
public static void Main()
{
int N = 5;
Console.Write(findTerm(N));
}
} // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // Javascript program to implement // the above approach // Function to return // Nth term of the series function findTerm(N)
{ return Math.pow(2, N) - 1;
} // Driver Code let N = 5; document.write(findTerm(N)); // This code is contributed by saurabh_jaiswal. </script> |
Output
31
Time Complexity: O(logN) since using inbuilt pow function
Auxiliary Space: O(1)