📌  相关文章
📜  找出系列1的第N个项2 + 6 + 15 + 31 + 56 +…

📅  最后修改于: 2021-04-27 22:45:38             🧑  作者: Mango

给定一个整数N  。任务是编写一个程序来查找给定序列的第N个术语:

1 + 2 + 6 + 15 + 31 + 56 + ... 

例子

Input : N = 8
Output : 141

Input : N = 20
Output : 2471

方法:
给定的序列是:

1, 2, 6, 15, 31, 56, 92, 141, ...

连续第一个差异

1 4 9 16 25 36 49 .......

连续第二个差异

3 5 7 9 11 13......

由于第二个连续差在AP中,所以序列的第n个项(t n )的形式为
A(n – 1)(n – 2)(n – 3)+ B(n – 1)(n – 2)+ C(n – 1)+ D
因此,t n = A(n – 1)(n – 2)(n – 3)+ B(n – 1)(n – 2)+ C(n – 1)+ D
现在,
t 1 = D = 1
t 2 = C(2-1)+ D = 2
t 3 = 2B + 2C + D = 6
t 4 = CA + 6B + 3C + D = 15
在求解上述四个方程式时,我们得到=> A = 1/3,B = 3/2,C = 1,D =1。代入这些值t n并经过简化,我们得到:
t_{n} = \frac{2n^{3}-3n^{2}+n+6}{6}
下面是上述方法的实现:

C++
// C++ program to find Nth
// term of the series:
// 1 + 2 + 6 + 15 + 31 + 56 + ...
#include
#include
using namespace std;
 
// calculate Nth term of given series
int Nth_Term(int n)
{
    return (2 * pow(n, 3) - 3 *
                pow(n, 2) + n + 6) / 6;
}
 
// Driver code
int main()
{
    int N = 8;
    cout << Nth_Term(N);
}


Java
// Java program to find Nth
// term of the series:
// 1 + 2 + 6 + 15 + 31 + 56 + ...
import java.util.*;
import java.lang.*;
 
class GFG
{
// calculate Nth term of given series
static double Nth_Term(int n)
{
    return (2 * Math.pow(n, 3) - 3 *
                Math.pow(n, 2) + n + 6) / 6;
}
 
// Driver code
static public void main (String args[])
{
    int N = 8;
    System.out.println(Nth_Term(N));
}
}
 
// This code is contributed
// by Akanksha Rai


Python3
# Python program to find Nth term of the series:
# 1 + 2 + 6 + 15 + 31 + 56 + ...
 
# calculate Nth term of given series
def Nth_Term(n):
    return (2 * pow(n, 3) - 3 * pow(n, 2) + n + 6) // 6
 
# Driver code
N = 8
print(Nth_Term(N))


C#
// C# program to find Nth
// term of the series:
// 1 + 2 + 6 + 15 + 31 + 56 + ...
using System;
 
class GFG
{
// calculate Nth term of given series
static double Nth_Term(int n)
{
    return (2 * Math.Pow(n, 3) - 3 *
                Math.Pow(n, 2) + n + 6) / 6;
}
 
// Driver code
static public void Main ()
{
    int N = 8;
    Console.WriteLine(Nth_Term(N));
}
}
 
// This code is contributed
// by Sach_Code


PHP


Javascript


输出:
141