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 seriesint findTerm(int N){ return pow(2, N) - 1;}// Driver Codeint main(){ int N = 5; cout << findTerm(N); return 0;} |
Java
// Java program to implement// the above approachimport 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 seriesdef findTerm(N): return pow(2, N) - 1# Driver CodeN = 5print(findTerm(N))# This code is contributed by samim2000. |
C#
// C# program to implement// the above approachusing 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 seriesfunction findTerm(N){ return Math.pow(2, N) - 1;}// Driver Codelet N = 5;document.write(findTerm(N));// This code is contributed by saurabh_jaiswal.</script> |
31
Time Complexity: O(logN) since using inbuilt pow function
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Info on that Topic: geeksforgeeks.org/find-the-nth-term-of-the-series-1-3-7-15-31/ […]
… [Trackback]
[…] Find More Information here to that Topic: geeksforgeeks.org/find-the-nth-term-of-the-series-1-3-7-15-31/ […]