找到系列 3,10,21,36,55... 的第 N 项
给定一个正整数N ,任务是找到序列的第 N项
3, 10, 21, 36, 55…till N terms
例子:
Input: N = 4
Output: 36
Input: N = 6
Output: 78
方法:
从给定的系列中,找到第 N项的公式-
1st term = 1 * ( 2(1) + 1 ) = 3
2nd term = 2 * ( 2(2) + 1 ) = 10
3rd term = 3 * ( 2(3) + 1 ) = 21
4th term = 4 * ( 2(4) + 1 ) = 36
.
.
Nth term = N * ( 2(N) + 1 )
给定系列的第 N 项可以概括为-
TN = N * ( 2(N) + 1 )
插图:
Input: N = 10
Output: 210
Explanation:
TN = N * ( 2(N) + 1 )
= 10 * ( 2(10) + 1 )
= 210
以下是上述方法的实现 -
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to return
// Nth term of the series
int nTh(int n)
{
return n * (2 * n + 1);
}
// Driver code
int main()
{
int N = 10;
cout << nTh(N) << endl;
return 0;
}
C
// C program to implement
// the above approach
#include
// Function to return
// Nth term of the series
int nTh(int n)
{
return n * (2 * n + 1);
}
// Driver code
int main()
{
// Value of N
int N = 10;
printf("%d", nTh(N));
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG {
// Driver code
public static void main(String[] args)
{
int N = 10;
System.out.println(nTh(N));
}
// Function to return
// Nth term of the series
public static int nTh(int n)
{
return n * (2 * n + 1);
}
}
Python
# Python program to implement
# the above approach
# Function to return
# Nth term of the series
def nTh(n):
return n * (2 * n + 1)
# Driver code
N = 10
print(nTh(N))
# This code is contributed by Samim Hossain Mondal.
C#
using System;
public class GFG
{
// Function to return
// Nth term of the series
public static int nTh(int n)
{
return n * (2 * n + 1);
}
static public void Main (){
// Code
int N = 10;
Console.Write(nTh(N));
}
}
// This code is contributed by Potta Lokesh
Javascript
输出
210
时间复杂度: O(1)
辅助空间: O(1)