📌  相关文章
📜  查找序列第n个项的程序1 4 15 24 45 60 92

📅  最后修改于: 2021-04-26 09:02:14             🧑  作者: Mango

给定数字n,任务是找到级数的第n个项

其中0 例子:

Input: n = 10
Output: 180

Input: n = 5
Output: 45

方法:
这个想法很简单,但很难识别。
如果n为奇数,则第n个项将为[(2 *(n ^ 2))– n]。
如果n为偶数,则第n个项将为[2 *((n ^ 2)– n)]。

执行:

C++
#include 
 
// function to calculate nth term of the series
long long int nthTerm(long long int n)
{
    // variable nth will store the nth term of series
    long long int nth;
 
    // if n is even
    if (n % 2 == 0)
        nth = 2 * ((n * n) - n);
 
    // if n is odd
    else
        nth = (2 * n * n) - n;
 
    // return nth term
    return nth;
}
 
// Driver code
int main()
{
    long long int n;
 
    n = 5;
 
    printf("%lld\n", nthTerm(n));
 
    n = 25;
    printf("%lld\n", nthTerm(n));
 
    n = 25000000;
    printf("%lld\n", nthTerm(n));
 
    n = 250000007;
    printf("%lld\n", nthTerm(n));
 
    return 0;
}


Java
// Java implementation of the above approach
 
class GFG
{
 
    // function to calculate nth
    // term of the series
    static long nthTerm(long n)
    {
        // variable nth will store the
        // nth term of series
        long nth;
     
        // if n is even
        if (n % 2 == 0)
            nth = 2 * ((n * n) - n);
     
        // if n is odd
        else
            nth = (2 * n * n) - n;
     
        // return nth term
        return nth;
    }
     
    // Driver code
    public static void main(String []args)
    {
        long n;
        n = 5;
        System.out.println(nthTerm(n));
     
        n = 25;
        System.out.println(nthTerm(n));
     
        n = 25000000;
        System.out.println(nthTerm(n));
     
        n = 250000007;
        System.out.println(nthTerm(n));
    }
}
 
// This code is contributed by Ryuga


Python3
# function to calculate nth term of the series
def nthTerm(n):
 
    # variable nth will store the nth
    # term of series
    nth = 0
 
    # if n is even
    if (n % 2 == 0):
        nth = 2 * ((n * n) - n)
 
    # if n is odd
    else:
        nth = (2 * n * n) - n
 
    # return nth term
    return nth
 
# Driver code
n = 5
 
print(nthTerm(n))
 
n = 25
print(nthTerm(n))
 
n = 25000000
print(nthTerm(n))
 
n = 250000007
print(nthTerm(n))
 
# This code is contributed by
# Mohit kumar 29


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
    // function to calculate nth
    // term of the series
    static long nthTerm(long n)
    {
        // variable nth will store the
        // nth term of series
        long nth;
     
        // if n is even
        if (n % 2 == 0)
            nth = 2 * ((n * n) - n);
     
        // if n is odd
        else
            nth = (2 * n * n) - n;
     
        // return nth term
        return nth;
    }
     
    // Driver code
    public static void Main()
    {
        long n;
        n = 5;
        Console.WriteLine(nthTerm(n));
     
        n = 25;
        Console.WriteLine(nthTerm(n));
     
        n = 25000000;
        Console.WriteLine(nthTerm(n));
     
        n = 250000007;
        Console.WriteLine(nthTerm(n));
    }
}
 
// This code is contributed by chandan_jnu


PHP


Javascript


输出:
45
1225
1249999950000000
125000006750000091