Dado un entero positivo N , ¡la tarea es encontrar la suma de la serie 1! – 2! + 3! – 4! + 5!… hasta el N-ésimo término.
Ejemplos:
Entrada: N = 6
Salida: -619
Explicación: ¡ La suma de la serie hasta el quinto término se puede calcular como
1! – 2! + 3! – 4! + 5! -6! = 1 -2 +6 -24 +120 -720 = -619Entrada: N = 5
Salida: 101
Enfoque Nativo: La forma más sencilla de resolver este problema es encontrar el factorial de todos los números en el rango [1, N] y calcular su suma con sus respectivos signos positivo y negativo, es decir, la posición impar será (+) ve y par la posición será negativa.
A continuación se muestra la implementación del enfoque anterior:
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
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)
Enfoque eficiente: la solución anterior se puede optimizar manteniendo el valor del factorial del número anterior y calculando el factorial del número actual usando ese valor y calculando su suma con su respectivo signo positivo y negativo.
A continuación se muestra la implementación del enfoque anterior:
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
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por athakur42u y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA