Saturday, January 11, 2025
Google search engine
HomeData Modelling & AIFind the Nth term of the series 3, 7, 19, 55, 163,...

Find the Nth term of the series 3, 7, 19, 55, 163, . . .

Given a positive integer N. The task is to find Nth term of the series 3, 7, 19, 55, 163, …..

Examples:

Input: N = 5
Output: 163

Input: N = 1
Output: 3

 

Approach: The sequence is formed by using the following pattern. For any value N 

TN = 2 * 3N – 1 + 1

Illustration:

Input: N = 5
Output: 163
Explanation:
TN = 2 * 3N – 1 + 1
     = 2 * 35 – 1 + 1
     = 2 * 81 + 1
     = 162 + 1
     = 163

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 calcNum(int N)
{
    return 2 * pow(3, N - 1) + 1;
}
 
// Driver Code
int main()
{
    int N = 5;
    cout << calcNum(N);
    return 0;
}


Java




// Java code to implement the above approach
import java.lang.*;
public class gfg
{
     
    /* Function to return the Nth term of the series */
    static int calcNum(int N)
    {
        return (int)(2*(Math.pow(3,N-1))) + 1 ;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
 
        System.out.println(calcNum(N));
    }
}
 
// This code is contributed by Abhishek Thakur


Python3




# Python3 program to implement
# the above approach
 
# Function to return Nth term
# of the series
def calcNum(N):
     
    return 2 * (3 ** (N - 1)) + 1
 
# Driver Code
N = 5
 
print(calcNum(N))
 
# This code is contributed by gfgking


C#




// C# code to implement the above approach
using System;
public class gfg
{
 
  /* Function to return the Nth term of the series */
  static int calcNum(int N)
  {
    return (int)(2 * (Math.Pow(3, N - 1))) + 1;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int N = 5;
 
    Console.WriteLine(calcNum(N));
  }
}
 
// This code is contributed by Abhishek Thakur


Javascript




<script>
      // JavaScript code for the above approach
 
 
      // Function to return Nth term
      // of the series
      function calcNum(N) {
          return 2 * Math.pow(3, N - 1) + 1;
      }
 
      // Driver Code
 
      let N = 5;
      document.write(calcNum(N));
 
// This code is contributed by Potta Lokesh
  </script>


Output

163

Time Complexity: O(logN) because 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!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
16 Aug, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments