📜  求序列 1^3/1+(1^3+2^3)/(1+3)+(1^3+2^3+3^3)/(1+3+5)+ 的第 N 项…

📅  最后修改于: 2022-05-13 01:56:08.476000             🧑  作者: Mango

求序列 1^3/1+(1^3+2^3)/(1+3)+(1^3+2^3+3^3)/(1+3+5)+ 的第 N 项…

给定一个正整数N 。任务是找到系列的第 N项:

例子:

方法:

从给定的系列中,找到第 N项的公式:

推导:

给定序列的第 N项可以概括为:

插图:

下面是上述问题的实现:

C++
// C++ program to find N-th term
// in the series
#include 
using namespace std;
 
// Function to find N-th term
// in the series
double nthTerm(int N)
{
    return (pow(N, 2) +
            2 * N + 1) / 4;
}
 
// Driver Code
int main()
{
    // Get the value of N
    int N = 5;
    cout << nthTerm(N);
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
class GFG {
 
    // Function to find N-th term
    // in the series
    static double nthTerm(int N)
    {
        return (Math.pow(N, 2) + 2 * N + 1) / 4;
    }
    public static void main(String[] args)
    {
        // Get the value of N
        int N = 5;
 
        System.out.println(nthTerm(N));
    }
}
 
// This code is contributed by Potta Lokesh


Python
# python 3 program for the above approach
import sys
 
# Function to find N-th term
# in the series
def nthTerm(N):
 
    return (pow(N, 2) + 2 * N + 1) / 4
 
# Driver Code
if __name__ == "__main__":
       
     N = 5
     print(nthTerm(N))
 
    # This code is contributed by hrithikgarg03188


C#
// C# program to find N-th term
// in the series
using System;
class GFG
{
 
  // Function to find N-th term
  // in the series
  static double nthTerm(int N)
  {
    return (Math.Pow(N, 2) +
            2 * N + 1) / 4;
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Get the value of N
    int N = 5;
    Console.Write(nthTerm(N));
  }
}
 
// This code is contributed by Samim Hosdsain Mondal.


Javascript


输出
9

时间复杂度: O(1)

辅助空间: O(1)