求系列 2, 8, 18, 32, 50, 的第 n 项。 . . .
给定一个整数N ,任务是找到序列的第 N 项
2, 8, 18, 32, 50, ….till N terms
例子:
Input: N = 4
Output: 32
Input: N = 6
Output: 72
方法:
从给定的系列中,找到第 N项的公式-
1st term = 2 * 1 ^ 2 = 2
2nd term = 2 * 2 ^ 2 = 8
3rd term = 2 * 3 ^ 2 = 18
4th term = 2 * 4 ^ 2 = 32
.
.
Nth term = 2 * N ^ 2
给定系列的第 N项可以概括为-
TN = 2 * N ^ 2
插图:
Input: N = 6
Output: 72
Explanation:
TN= 2 * N ^ 2
= 2 * 6 ^ 2
= 72
以下是上述方法的实现 -
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to return nth
// term of the series
int find_nth_Term(int n)
{
return 2 * n * n;
}
// Driver code
int main()
{
// Value of N
int N = 6;
// function call
cout << find_nth_Term(N) <<
end;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to return nth
// term of the series
static int find_nth_Term(int n)
{
return 2 * n * n;
}
// Driver code
public static void main (String[] args)
{
// Value of N
int N = 6;
// function call
System.out.println(find_nth_Term(N));
}
}
// This code is contributed by hrithikgarg03188.
Python
# Python program to implement
# the above approach
# Find n-th term of series
# 2, 8, 18, 32, 50...
def nthTerm(n):
return 2 * n * n;
# Driver Code
if __name__ == "__main__":
# Value of N
N = 6
# function call
print(nthTerm(N))
# This code is contributed by Samim Hossain Mondal.
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to return nth
// term of the series
static int find_nth_Term(int n)
{
return 2 * n * n;
}
// Driver Code
public static void Main()
{
// Value of N
int N = 6;
// function call
Console.Write(find_nth_Term(N));
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
72
时间复杂度: O(1)
辅助空间: O(1)