Given a positive integer N. The task is to find Nth term of the series 3, 7, 14, 27, 52, …..
Examples:
Input: N = 5
Output: 52Input: N = 1
Output: 3
Approach:
The sequence is formed by using the following pattern. For any value N-
TN = (N -1) + 3 * 2N-1
Illustration:
Input: N = 5
Output: 52
Explanation:
TN = (5 – 1) + 3 * 25 – 1
= 4 + 3 * 16
= 52
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 ((N - 1) + 3 *
pow (2, N - 1));
} // Driver Code int main()
{ int N = 5;
cout << calcNum(N);
return 0;
} |
Java
// Java program to implement // the above approach class GFG
{ // Function to return Nth term
// of the series
static int calcNum( int N) {
return ( int ) ((N - 1 ) + 3 * Math.pow( 2 , N - 1 ));
}
// Driver Code
public static void main(String args[]) {
int N = 5 ;
System.out.println(calcNum(N));
}
} // This code is contributed by saurabh_jaiswal. |
Python3
# Python code for the above approach # Function to return Nth term # of the series def calcNum(N):
return ((N - 1 ) + 3 * ( 2 * * (N - 1 )));
# Driver Code N = 5 ;
print (calcNum(N));
# This code is contributed by Saurabh Jaiswal |
C#
// C# program to implement // the above approach using System;
class GFG {
// Function to return Nth term
// of the series
static int calcNum( int N)
{
return ( int )((N - 1) + 3 * Math.Pow(2, N - 1));
}
// Driver Code
public static void Main()
{
int N = 5;
Console.WriteLine(calcNum(N));
}
} // This code is contributed by ukasp. |
Javascript
<script>
// JavaScript code for the above approach
// Function to return Nth term
// of the series
function calcNum(N) {
return ((N - 1) + 3 *
Math.pow(2, N - 1));
}
// Driver Code
let N = 5;
document.write(calcNum(N));
// This code is contributed by Potta Lokesh </script>
|
Output
52
Time Complexity: O(logn)
Auxiliary Space: O(1), since no extra space has been taken.