Thursday, October 10, 2024
Google search engine
HomeData Modelling & AIFind nth term of 0, 9, 24, 45, . . . .

Find nth term of 0, 9, 24, 45, . . . .

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: 45

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




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




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


Python3




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




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


Javascript




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


Output

105

Time Complexity: O(1)

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 :
01 Feb, 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