📌  相关文章
📜  系列1,17,98,354的n个项……

📅  最后修改于: 2021-05-05 03:19:20             🧑  作者: Mango

给定一个系列1,17,98,354……找到该系列的第n个术语。

该级数基本上表示前n个自然数的4次幂之和。第一项是1 4的总和。第二项是两个数字的和,即(1 4 + 2 4 = 17),第三项是(1 4 + 2 4 + 3 4 = 98),依此类推。

例子 :

Input : 5
Output : 979

Input : 7
Output : 4676

天真的方法:
一个简单的解决方案是将前n个自然数的4次幂相加。通过使用迭代,我们可以轻松地找到级数的n个项。

下面是上述方法的实现:

C++
// CPP program to find n-th term of
// series
#include 
using namespace std;
  
// Function to find the nth term of series
int sumOfSeries(int n)
{
    // Loop to add 4th powers
    int ans = 0;
    for (int i = 1; i <= n; i++)
        ans += i * i * i * i;
  
    return ans;
}
  
// Driver code
int main()
{
    int n = 4;
    cout << sumOfSeries(n);
    return 0;
}


Java
// Java program to find n-th term of
// series
import java.io.*;
  
class GFG {
  
    // Function to find the nth term of series
    static int sumOfSeries(int n)
    {
        // Loop to add 4th powers
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans += i * i * i * i;
  
        return ans;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int n = 4;
        System.out.println(sumOfSeries(n));
    }
}


Python3
# Python 3 program to find
# n-th term of
# series
   
       
# Function to find the
# nth term of series
def sumOfSeries(n) :
    # Loop to add 4th powers
    ans = 0
    for i in range(1, n + 1) :
        ans = ans + i * i * i * i 
        
    return ans
   
   
# Driver code
n = 4
print(sumOfSeries(n))


C#
// C# program to find n-th term of
// series
using System;
class GFG {
  
    // Function to find the 
    // nth term of series
    static int sumOfSeries(int n)
    {
          
        // Loop to add 4th powers
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans += i * i * i * i;
  
        return ans;
    }
  
    // Driver code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(sumOfSeries(n));
    }
}
  
// This code is contributed by anuj_67


PHP


C++
// CPP program to find the n-th
// term in series
#include 
using namespace std;
  
// Function to find nth term
int sumOfSeries(int n)
{
    return n * (n + 1) * (6 * n * n * n
                 + 9 * n * n + n - 1) / 30;
}
  
// Driver code
int main()
{
    int n = 4;
    cout << sumOfSeries(n);
    return 0;
}


Java
// Java program to find the n-th
// term in series
import java.io.*;
  
class Series {
  
    // Function to find nth term
    static int sumOfSeries(int n)
    {
        return n * (n + 1) * (6 * n * n * n 
                    + 9 * n * n + n - 1) / 30;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(sumOfSeries(n));
    }
}


Python
# Python program to find the Nth 
# term in series
   
# Function to print nth term 
# of series 
def sumOfSeries(n):
    return n * (n + 1) * (6 * n * n * n 
                 + 9 * n * n + n - 1)/ 30
       
# Driver code 
n = 4
print sumOfSeries(n)


C#
// C# program to find the n-th
// term in series
using System;
class Series {
  
    // Function to find nth term
    static int sumOfSeries(int n)
    {
        return n * (n + 1) * (6 * n * n * n 
                  + 9 * n * n + n - 1) / 30;
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(sumOfSeries(n));
    }
}
  
// This code is contributed by anuj_67


PHP


输出 :
354

时间复杂度: O(n)。

高效的方法:
该系列中的模式是第n个项等于第(n-1)个项与n 4的和

例子 :

n = 2
2nd term equals to sum of 1st term and 24 i.e 16
A2 = A1 + 16 
   = 1 + 16
   = 17

Similarly,
A3 = A2 + 34
   = 17 + 81
   = 98 and so on..

我们得到:

A(n) = A(n - 1) + n4 
     = A(n - 2) + n4  + (n-1)4 
     = A(n - 3) + n4  + (n-1)4 + (n-2)4
       .
       .
       .
     = A(1) + 16 + 81... + (n-1)4 + n4

A(n) = 1 + 16 + 81 +... + (n-1)4 + n4
     = n(n + 1)(6n3 + 9n2 + n - 1) / 30 

i.e A(n) is sum of 4th powers of First n natural numbers.

下面是上述方法的实现:

C++

// CPP program to find the n-th
// term in series
#include 
using namespace std;
  
// Function to find nth term
int sumOfSeries(int n)
{
    return n * (n + 1) * (6 * n * n * n
                 + 9 * n * n + n - 1) / 30;
}
  
// Driver code
int main()
{
    int n = 4;
    cout << sumOfSeries(n);
    return 0;
}

Java

// Java program to find the n-th
// term in series
import java.io.*;
  
class Series {
  
    // Function to find nth term
    static int sumOfSeries(int n)
    {
        return n * (n + 1) * (6 * n * n * n 
                    + 9 * n * n + n - 1) / 30;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(sumOfSeries(n));
    }
}

Python

# Python program to find the Nth 
# term in series
   
# Function to print nth term 
# of series 
def sumOfSeries(n):
    return n * (n + 1) * (6 * n * n * n 
                 + 9 * n * n + n - 1)/ 30
       
# Driver code 
n = 4
print sumOfSeries(n)

C#

// C# program to find the n-th
// term in series
using System;
class Series {
  
    // Function to find nth term
    static int sumOfSeries(int n)
    {
        return n * (n + 1) * (6 * n * n * n 
                  + 9 * n * n + n - 1) / 30;
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(sumOfSeries(n));
    }
}
  
// This code is contributed by anuj_67

的PHP


输出:
354

时间复杂度: O(1)。