📌  相关文章
📜  求系列 1 的总和! – 2! + 3! – 4! + 5! . . .直到第 N 个学期

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

求系列 1 的总和! – 2! + 3! – 4! + 5! . . .直到第 N 个学期

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

例子:

Native Approach:解决这个问题最简单的方法是找到[1,N]范围内所有数字的阶乘,并用它们各自的正负号计算它们的和,即奇数位置将是(+)ve和偶数位置将为负。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
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


Python
# Python program of the above approach
 
# Function to find factorial
# of a given number
def factorial(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
def SeriesSum(N):
     
    # Stores Required Sum
    Sum = 0
 
    # Loop to calculate factorial
    # and sum them with their
    # respective sign
    for i in range(1, N + 1):
 
        # Factorial of cur integer
        fact = factorial(i);
 
        # Stores the sign
        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
 
N = 6
print(SeriesSum(N))
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# implementation for the above approach
using System;
 
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()
{
    int N = 6;
     
    Console.Write(SeriesSum(N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ program of the above approach
#include 
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;
}


Java
// Java program of the above approach
 
import java.util.*;
 
class GFG{
 
// Function to find the sum of
// the series 1! - 2! + 3! - 4!
// + 5!... till the Nth term
static 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
public static void main(String[] args)
{
    int N = 6;
    System.out.print(SeriesSum(N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program of the above approach
 
# Function to find the sum of
# the series 1! - 2! + 3! - 4!
# + 5!... till the Nth term
def SeriesSum(N):
 
    # Stores the required sum
    # and factorial value
    Sum = 0
    fact = 1
 
    # Loop to calculate factorial
    # and sum them with their
    # respective sign
    for i in range(1, N + 1):
 
        # Calculate factorial
        fact = fact * i
 
        # Stores the sign
        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
if __name__ == "__main__":
 
    N = 6
    print(SeriesSum(N))
 
    # This code is contributed by ukasp.


C#
// C# program of the above approach
using System;
 
public class GFG{
 
// Function to find the sum of
// the series 1! - 2! + 3! - 4!
// + 5!... till the Nth term
static 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
public static void Main(String[] args)
{
    int N = 6;
    Console.Write(SeriesSum(N));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
-619

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

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

下面是上述方法的实现:

C++

// C++ program of the above approach
#include 
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;
}

Java

// Java program of the above approach
 
import java.util.*;
 
class GFG{
 
// Function to find the sum of
// the series 1! - 2! + 3! - 4!
// + 5!... till the Nth term
static 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
public static void main(String[] args)
{
    int N = 6;
    System.out.print(SeriesSum(N));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python 3 program of the above approach
 
# Function to find the sum of
# the series 1! - 2! + 3! - 4!
# + 5!... till the Nth term
def SeriesSum(N):
 
    # Stores the required sum
    # and factorial value
    Sum = 0
    fact = 1
 
    # Loop to calculate factorial
    # and sum them with their
    # respective sign
    for i in range(1, N + 1):
 
        # Calculate factorial
        fact = fact * i
 
        # Stores the sign
        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
if __name__ == "__main__":
 
    N = 6
    print(SeriesSum(N))
 
    # This code is contributed by ukasp.

C#

// C# program of the above approach
using System;
 
public class GFG{
 
// Function to find the sum of
// the series 1! - 2! + 3! - 4!
// + 5!... till the Nth term
static 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
public static void Main(String[] args)
{
    int N = 6;
    Console.Write(SeriesSum(N));
}
}
 
// This code is contributed by 29AjayKumar

Javascript


输出
-619

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