Open In App

Sum of Alternating Factorial Series

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, find the sum of Alternating Factorial Series till Nth term. The Alternating Factorial Series is a mathematical series where the sign of each term alternates between the positive and negative and terms are factorials of the integers. The general form of an alternating factorial series can be represented as:

S = 1! − 2! + 3! − 4! + … + (−1)(N+1)⋅N!

Examples:

Input: N = 4
Output: -19
Explanation: The first four terms of alternating factorial series are: 1, -2, 6 and -24 and the total sum = 1 – 2 + 6 – 24 = -19

Input: N = 5
Output: 101
Explanation: The first five terms of alternating factorial series are: 1, -2, 6, -24 and 120 and the total sum = 1 – 2 + 6 – 24 + 120 = 101

Approach: To solve the problem, follow the below idea:

In this approach, we use a loop to iterate through each term of the alternating factorial series and accumulate the sum. In order to calculate each term of the series efficiently, for every iteration i we store the previous term and multiply it with i to get the next term. To keep track of the sign, we can keep a sign variable = 1 and toggle it (multiply it with -1) after each term.

Step-by-step algorithm:

  • Maintain a variable, say sum to store the sum of alternating factorial series, another variable sign to keep track of the sign.
  • Initialize the starting term, term = 1
  • Iterate from i = 1 to N
    • Multiple i with term to get the current term.
    • Add (sign * term) to the sum.
    • Toggle the sign by multiplying it with -1.
  • After N iterations, return sum as the final answer.

Below is the implementation of the algorithm:

C++
#include <iostream>
#define ll long long
using namespace std;

// function to calculate the sum of alternating factorial
// series till Nth term
ll getSum(ll N)
{
    ll sum = 0, sign = 1;
    ll term = 1;
    // loop to calculate the sum of the series
    for (int i = 1; i <= N; i++) {
        term = i * term;
        // Add the current term to the sum
        sum += sign * term;
        // Toggle the sign for the next term
        sign *= -1;
    }
    return sum;
}

int main()
{
    ll N = 4;

    // Output the result
    cout << getSum(N) << endl;
    return 0;
}
Java
public class Main {
    // Function to calculate the sum of alternating factorial
    // series till Nth term
    static long getSum(int N) {
        long sum = 0, sign = 1;
        long term = 1;
        // Loop to calculate the sum of the series
        for (int i = 1; i <= N; i++) {
            term = i * term;
            // Add the current term to the sum
            sum += sign * term;
            // Toggle the sign for the next term
            sign *= -1;
        }
        return sum;
    }

    public static void main(String[] args) {
        int N = 4;

        // Output the result
        System.out.println(getSum(N));
    }
}

// This code is contributed by shivamgupta0987654321
Python3
# Define a function to calculate the sum of alternating factorial series till Nth term
def getSum(N):
    # Initialize sum, sign, and term
    sum = 0
    sign = 1
    term = 1
    # Loop to calculate the sum of the series
    for i in range(1, N+1):
        term = i * term
        # Add the current term to the sum
        sum += sign * term
        # Toggle the sign for the next term
        sign *= -1
    return sum

# Set N to 4
N = 4

print(getSum(N))
C#
using System;

public class GFG
{
    // Function to calculate the sum of alternating factorial
    // series till Nth term
    static long GetSum(int N)
    {
        long sum = 0, sign = 1;
        long term = 1;
        // Loop to calculate the sum of the series
        for (int i = 1; i <= N; i++)
        {
            term = i * term;
            // Add the current term to the sum
            sum += sign * term;
            // Toggle the sign for the next term
            sign *= -1;
        }
        return sum;
    }

    public static void Main(string[] args)
    {
        int N = 4;

        // Output the result
        Console.WriteLine(GetSum(N));
    }
}
JavaScript
function getSum(N) {
    // Initialize sum, sign, and term
    let sum = 0;
    let sign = 1;
    let term = 1;
    
    // Loop to calculate the sum of the series
    for (let i = 1; i <= N; i++) {
        term = i * term;
        // Add the current term to the sum
        sum += sign * term;
        // Toggle the sign for the next term
        sign *= -1;
    }
    return sum;
}

// Set N to 4
const N = 4;

console.log(getSum(N));

Output
-19

Time Complexity: O(N), where N is the number of terms in the alternating factorial series.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads