Given a natural number N, the task is to find the Nth term of the series
0, 9, 24, 45, . . . .till N terms
Examples:
Input: N = 4
Output: 45Input: N = 6
Output: 105
Approach:
From the given series, find the formula for Nth term-
1st term = 3 * 1 * 1 – 3 = 0
2nd term = 3 * 2 * 2 – 3 = 9
3rd term = 3 * 3 * 3 – 3 = 24
4th term = 3 * 4 * 4 – 3 = 45
.
.
Nth term = 3 * N * N – 3
The Nth term of the given series can be generalized as-
TN = 3 * N * N – 3
Illustration:
Input: N = 6
Output: 105
Explanation:
TN = 3 * N * N – 3
= 3 * 6 * 6 – 3
= 108 – 3
= 105
Below is the implementation of the above approach-
// C++ program to implement // the above approach #include <iostream> using namespace std;
// Function to return nth // term of the series int nth_Term( int n)
{ return 3 * n * n - 3;
} // Driver code int main()
{ // Value of N
int N = 6;
// Invoke function to find
// Nth term
cout << nth_Term(N) <<
endl;
return 0;
} |
// Java program to implement // the above approach import java.util.*;
public class GFG
{ // Function to return nth
// term of the series
static int nth_Term( int n)
{
return 3 * n * n - 3 ;
}
// Driver code
public static void main(String args[])
{
// Value of N
int N = 6 ;
// Invoke function to find
// Nth term
System.out.println(nth_Term(N));
}
} // This code is contributed by Samim Hossain Mondal. |
# Python code for the above approach # Function to return nth # term of the series def nth_Term(n):
return 3 * n * n - 3 ;
# Driver code # Value of N N = 6 ;
# Invoke function to find # Nth term print (nth_Term(N))
# This code is contributed by gfgking |
// C# program to implement // the above approach using System;
class GFG
{ // Function to return nth // term of the series static int nth_Term( int n)
{ return 3 * n * n - 3;
} // Driver code public static void Main()
{ // Value of N
int N = 6;
// Invoke function to find
// Nth term
Console.WriteLine(nth_Term(N));
} } // This code is contributed by Samim Hossain Mondal. |
<script> // JavaScript code for the above approach
// Function to return nth
// term of the series
function nth_Term(n) {
return 3 * n * n - 3;
}
// Driver code
// Value of N
let N = 6;
// Invoke function to find
// Nth term
document.write(nth_Term(N) + '<br>' )
// This code is contributed by Potta Lokesh
</script>
|
105
Time Complexity: O(1)
Auxiliary Space: O(1)