📜  求 0, 9, 24, 45, 的第 n 项。 . . .

📅  最后修改于: 2022-05-13 01:56:08.746000             🧑  作者: Mango

求 0, 9, 24, 45, 的第 n 项。 . . .

给定一个自然数N ,任务是找到序列的第 N 项

例子:

方法:

从给定的系列中,找到第 N项的公式-

给定系列的第 N项可以概括为-

插图:

以下是上述方法的实现 -

C++
// C++ program to implement
// the above approach
#include 
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


输出
105

时间复杂度: O(1)

辅助空间: O(1)