求数列 1 的和!– 2!+ 3!– 4!+ 5!。。。直到第 n 个学期

原文:https://www . geesforgeks . org/find-sum-the-series-1-2-3-4-5-till-n-term/

给定一个正整数 N ,任务是求数列 1 的和!– 2!+ 3!– 4!+ 5!…直到第 n 个学期。

示例:

输入: N = 6 输出: -619 说明:到第 5 项的级数之和可以计算为 1!– 2!+ 3!– 4!+ 5!-6!= 1 -2 +6 -24 +120 -720 = -619

输入:N = 5 T3】输出: 101

天真法:解决这个问题最简单的方法就是在【1,N】范围内找到所有数字的阶乘,用它们各自的正负符号计算它们的和,即奇数位置为(+)ve,偶数位置为负数。

下面是上述方法的实现:

C++

// C++ program of the above approach
#include <iostream>
using namespace std;

// Function to find factorial
// of a given number
int factorial(int N)
{
    if (N == 1) {
        return 1;
    }

    // Recursive Call
    return N * factorial(N - 1);
}

// Function to find the sum of
// the series 1! - 2! + 3! - 4!
// + 5!... till the Nth term
int SeriesSum(int N)
{
    // Stores Required Sum
    int Sum = 0;

    // Loop to calculate factorial
    // and sum them with their
    // respective sign
    for (int i = 1; i <= N; i++) {

        // Factorial of cur integer
        int fact = factorial(i);

        // Stores the sign
        int sign = fact;

        // If position is even sign
        // must be negative
        if (i % 2 == 0) {
            sign = sign * -1;
        }

        // Adding value in sum
        Sum += sign;
    }

    // Return Answer
    return Sum;
}

// Driver Code
int main()
{
    int N = 6;
    cout << SeriesSum(N);
    return 0;
}

Java 语言(一种计算机语言,尤用于创建网站)

// Java implementation for the above approach
import java.util.*;

class GFG{

// Function to find factorial
// of a given number
static int factorial(int N)
{
    if (N == 1)
    {
        return 1;
    }

    // Recursive Call
    return N * factorial(N - 1);
}

// Function to find the sum of
// the series 1! - 2! + 3! - 4!
// + 5!... till the Nth term
static int SeriesSum(int N)
{

    // Stores Required Sum
    int Sum = 0;

    // Loop to calculate factorial
    // and sum them with their
    // respective sign
    for(int i = 1; i <= N; i++)
    {

        // Factorial of cur integer
        int fact = factorial(i);

        // Stores the sign
        int sign = fact;

        // If position is even sign
        // must be negative
        if (i % 2 == 0)
        {
            sign = sign * -1;
        }

        // Adding value in sum
        Sum += sign;
    }

    // Return Answer
    return Sum;
}

// Driver Code
public static void main(String args[])
{
    int N = 6;

    System.out.print(SeriesSum(N));
}
}

// This code is contributed by sanjoy_62

Output

-619

时间复杂度:O(N2) 辅助空间: O(1)

有效方法:上述解决方案可以通过保持前一个数的阶乘的值,并使用该值计算当前数的阶乘,以及用它们各自的正负符号计算它们的和来优化。

下面是上述方法的实现:

C++

// C++ program of the above approach
#include <iostream>
using namespace std;

// Function to find the sum of
// the series 1! - 2! + 3! - 4!
// + 5!... till the Nth term
int SeriesSum(int N)
{
    // Stores the required sum
    // and factorial value
    int Sum = 0, fact = 1;

    // Loop to calculate factorial
    // and sum them with their
    // respective sign
    for (int i = 1; i <= N; i++) {

        // Calculate factorial
        fact = fact * i;

        // Stores the sign
        int sign = fact;

        // If position is even sign
        // must be negative
        if (i % 2 == 0) {
            sign = sign * -1;
        }

        // Adding value in sum
        Sum += sign;
    }

    // Return Answer
    return Sum;
}

// Driver Code
int main()
{
    int N = 6;
    cout << SeriesSum(N);
    return 0;
}

Output

-619

时间复杂度:O(N) T5辅助空间:** O(1)