求 0, 9, 24, 45, 的第 n 项。 . . .
给定一个自然数N ,任务是找到序列的第 N 项
0, 9, 24, 45, . . . .till N terms
例子:
Input: N = 4
Output: 45
Input: N = 6
Output: 105
方法:
从给定的系列中,找到第 N项的公式-
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
给定系列的第 N项可以概括为-
TN = 3 * N * N – 3
插图:
Input: N = 6
Output: 105
Explanation:
TN = 3 * N * N – 3
= 3 * 6 * 6 – 3
= 108 – 3
= 105
以下是上述方法的实现 -
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)