Given a positive integer N. The task is to find Nth term of the series:
4, 11, 30, 85, 248…
Examples:
Input: N = 4
Output: 85Input: N = 2
Output: 11
Approach:
The Nth term of the given series can be generalized as:
TN = 3N + N
Illustration:
Input: N = 4
Output: 85
Explanation:
TN = 3N +N
= 34 + 4
= 81 +4
= 85
Below is the implementation of the above problem:
C++
// C++ program to find N-th term // of the series- // 4, 11, 30, 85, 248... #include <bits/stdc++.h> using namespace std;
// Function to return Nth term // of the series int nthTerm( int N)
{ return pow (3, N) + N;
} // Driver Code int main()
{ // Get the value of N
int N = 4;
cout << nthTerm(N);
return 0;
} |
Java
// Java program to find N-th term // of the series- // 4, 11, 30, 85, 248... class GFG
{ // Function to return Nth term
// of the series
static int nthTerm( int N) {
return ( int )Math.pow( 3 , N) + N;
}
// Driver Code
public static void main(String args[])
{
// Get the value of N
int N = 4 ;
System.out.println(nthTerm(N));
}
} // This code is contributed by saurabh_jaiswal. |
Python3
# Python code for the above approach # Function to return Nth term # of the series def nthTerm(N):
return ( 3 * * N) + N;
# Driver Code # Get the value of N N = 4 ;
print (nthTerm(N));
# This code is contributed by Saurabh Jaiswal |
C#
// C# program to find N-th term // of the series- // 4, 11, 30, 85, 248... using System;
class GFG
{ // Function to return Nth term
// of the series
static int nthTerm( int N) {
return ( int )Math.Pow(3, N) + N;
}
// Driver Code
public static void Main()
{
// Get the value of N
int N = 4;
Console.Write(nthTerm(N));
}
} // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach
// Function to return Nth term
// of the series
function nthTerm(N) {
return Math.pow(3, N) + N;
}
// Driver Code
// Get the value of N
let N = 4;
document.write(nthTerm(N));
// This code is contributed by Potta Lokesh </script>
|
Output
85
Time Complexity: O(log3N)
Auxiliary Space: O(1), since no extra space has been taken.