Saturday, September 6, 2025
HomeData Modelling & AIFind the Nth term of the series 1, 3, 7, 15, 31...

Find the Nth term of the series 1, 3, 7, 15, 31 . . .

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++




// 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)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11805 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6754 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS