Given a positive integer N, the task is to find the sum of series 1! – 2! + 3! – 4! + 5!… till Nth term.
Examples:
Input: N = 6
Output: -619
Explanation: The sum of the series upto 5th term can be calculated as
1! – 2! + 3! – 4! + 5! -6! = 1 -2 +6 -24 +120 -720 = -619Input: N = 5
Output: 101
Native Approach: The simplest way to solve this problem is to find the factorial of all numbers in the range [1, N] and calculate their sum with their respective positive and negative sign, i.e., odd position will be (+)ve and even position will be negative.
Below is the implementation of the above approach:
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 |
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
<script> // JavaScript code for the above approach // Function to find factorial // of a given number function 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 function SeriesSum(N) { // Stores Required Sum let Sum = 0; // Loop to calculate factorial // and sum them with their // respective sign for (let i = 1; i <= N; i++) { // Factorial of cur integer let fact = factorial(i); // Stores the sign let 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 let N = 6; document.write(SeriesSum(N)); // This code is contributed by Potta Lokesh </script> |
-619
Time Complexity: O(N2)
Auxiliary Space: O(N) for call stack because using recursion
Efficient Approach: The above solution can be optimized by maintaining the value of the factorial of the previous number and calculating the factorial of the current number using that value and calculating their sum with their respective positive and negative sign.
Below is the implementation of the above approach:
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; } |
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
<script> // javascript program of the above approach // Function to find the sum of // the series 1! - 2! + 3! - 4! // + 5!... till the Nth term function SeriesSum(N) { // Stores the required sum // and factorial value var Sum = 0, fact = 1; // Loop to calculate factorial // and sum them with their // respective sign for ( var i = 1; i <= N; i++) { // Calculate factorial fact = fact * i; // Stores the sign var 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 var N = 6; document.write(SeriesSum(N)); // This code is contributed by 29AjayKumar </script> |
-619
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!