📌  相关文章
📜  系列1 * 1 * 2的总和! + 2 * 2 * 3! +…….. + n * n *(n + 1)!

📅  最后修改于: 2021-04-27 19:26:51             🧑  作者: Mango

给定n,我们需要找到1 * 1 * 2的总和! + 2 * 2 * 3! +…….. + n * n *(n + 1)!

例子:

我们可以假设不会发生溢出。

一个简单的解决方案是逐项计算项并加到结果中。

一个有效的解决方案是基于直接公式2 +(n * n + n – 2)*(n +1)!
公式的工作基于此帖子。

C++
// CPP program to find sum of the series.
#include 
using namespace std;
  
int factorial(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Function to calculate required series
int calculateSeries(int n)
{
    return 2 + (n * n + n - 2) * factorial(n + 1);
}
  
// Drivers code
int main()
{
    int n = 3;
    cout << calculateSeries(n);
    return 0;
}


Java
// java program to find sum of the series.
import java.io.*;
  
class GFG {
      
    static int factorial(int n)
    {
        int res = 1;
        for (int i = 2; i <= n; i++)
            res = res * i;
        return res;
    }
      
    // Function to calculate required series
    static int calculateSeries(int n)
    {
        return 2 + (n * n + n - 2) 
                      * factorial(n + 1);
    }
      
    // Drivers code
    public static void main (String[] args) 
    {
        int n = 3;
        System.out.println(calculateSeries(n));
    }
}
  
// This code is contributed by anuj_67.


Python3
# Python program to find sum of
# the series.
import math
  
def factorial(n):
    res = 1
    i = 2
    for i in (n+1):
        res = res * i
    return res
      
# Function to calculate required
# series
def calculateSeries(n):
    return (2 + (n * n + n - 2) 
        * math.factorial(n + 1))
  
# Driver code
n = 3
print(calculateSeries(n))
  
# This code is contributed by 
# Prateek bajaj


C#
// C# program to find sum of the series.
using System;
class GFG {
      
    static int factorial(int n)
    {
        int res = 1;
        for (int i = 2; i <= n; i++)
            res = res * i;
        return res;
    }
      
    // Function to calculate required series
    static int calculateSeries(int n)
    {
        return 2 + (n * n + n - 2) 
                 * factorial(n + 1);
    }
      
    // Driver code
    public static void Main () 
    {
        int n = 3;
        Console.WriteLine(calculateSeries(n));
    }
}
  
// This code is contributed by anuj_67.


PHP


输出:
242