Dado un entero positivo n, escribe una función para calcular la suma de la serie 1/1. + 1/2! + .. + 1/n!
Una solución simple es inicializar la suma como 0, luego ejecutar un ciclo y llamar a la función factorial dentro del ciclo.
A continuación se muestra la implementación de una solución simple.
C++
// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n! #include <iostream> using namespace std; // Utility function to find int factorial(int n) { int res = 1; for (int i=2; i<=n; i++) res *= i; return res; } // A Simple Function to return value of 1/1! + 1/2! + .. + 1/n! double sum(int n) { double sum = 0; for (int i = 1; i <= n; i++) sum += 1.0/factorial(i); return sum; } // Driver program to test above functions int main() { int n = 5; cout << sum(n); return 0; }
Java
// A simple Java program to compute // sum of series 1/1! + 1/2! + .. + 1/n! import java.io.*; class GFG { // Utility function to find static int factorial(int n) { int res = 1; for (int i = 2; i <= n; i++) res *= i; return res; } // A Simple Function to return value // of 1/1! + 1/2! + .. + 1/n! static double sum(int n) { double sum = 0; for (int i = 1; i <= n; i++) sum += 1.0/factorial(i); return sum; } // Driver program public static void main (String[] args) { int n = 5; System.out.println(sum(n)); } } // This code is contributed by Ajit.
Python3
# Python3 program to compute sum of series # 1/1! + 1/2! + .. + 1/n! # Function to find factorial of a number def factorial(n): res = 1 for i in range(2, n + 1): res *= i return res # A Simple Function to return value # of 1/1! + 1/2! + .. + 1/n! def sum(n): s = 0.0 for i in range(1, n + 1): s += 1.0 / factorial(i) print(s) # Driver program to test above functions n = 5 sum(n) # This code is contributed by Danish Raza
C#
// A simple C# program to compute sum // of series 1/1! + 1/2! + .. + 1/n! using System; class GFG { // Utility function to find static int factorial(int n) { int res = 1; for (int i = 2; i <= n; i++) res *= i; return res; } // A Simple Function to return value // of 1/1! + 1/2! + .. + 1/n! static double sum(int n) { double sum = 0; for (int i = 1; i <= n; i++) sum += 1.0/factorial(i); return sum; } // Driver program public static void Main () { int n = 5; Console.WriteLine(sum(n)); } } // This code is contributed by Sam007.
PHP
<?php // A simple PHP program to compute // sum of series 1/1! + 1/2! + .. + 1/n! // Utility function to find function factorial($n) { $res = 1; for ($i = 2; $i <= $n; $i++) $res *= $i; return $res; } // A Simple Function to return // value of 1/1! + 1/2! + .. + 1/n! function sum($n) { $sum = 0; for ($i = 1; $i <= $n; $i++) $sum += 1.0 / factorial($i); return $sum; } // Driver Code $n = 5; echo(sum($n)); // This code is contributed by Ajit. ?>
Javascript
<script> //Javascript program to compute // sum of series 1/1! + 1/2! + .. + 1/n! // Utility function to find function factorial(n) { let res = 1; for (let i = 2; i <= n; i++) res *= i; return res; } // A Simple Function to return // value of 1/1! + 1/2! + .. + 1/n! function sum(n) { let sum = 0; for (let i = 1; i <= n; i++) sum += 1.0 / factorial(i); return sum; } // Driver Code let n = 5; document.write(sum(n).toFixed(5)); // This code is contributed by sravan kumar </script>
Producción:
1.71667
Complejidad temporal: O(n * n)
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.
Una Solución Eficiente puede encontrar la suma en tiempo O(n). La idea es calcular el factorial en el mismo bucle que la suma. A continuación se muestra la implementación de esta idea.
C++
// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n! #include <iostream> using namespace std; // An Efficient Function to return value of 1/1! + 1/2! + .. + 1/n! double sum(int n) { double sum = 0; int fact = 1; for (int i = 1; i <= n; i++) { fact *= i; // Update factorial sum += 1.0/fact; // Update series sum } return sum; } // Driver program to test above functions int main() { int n = 5; cout << sum(n); return 0; }
Java
// A simple Java program to compute // sum of series 1/1! + 1/2! + .. + 1/n! import java.io.*; class GFG { // An Efficient Function to return // value of 1/1! + 1/2! + .. + 1/n! static double sum(int n) { double sum = 0; int fact = 1; for (int i = 1; i <= n; i++) { // Update factorial fact *= i; // Update series sum sum += 1.0/fact; } return sum; } // Driver program public static void main (String[] args) { int n = 5; System.out.println(sum(n)); } } // This code is contributed by Ajit.
Python3
# Python3 program to compute sum of series # 1/1! + 1/2! + .. + 1/n! # Function to return value of # 1/1! + 1/2! + .. + 1/n! def sum(n): sum = 0 fact = 1 for i in range(1, n + 1): # Update factorial fact *= i # Update series sum sum += 1.0/fact print(sum) # Driver program to test above functions n = 5 sum(n) # This code is contributed by Danish Raza
C#
// A simple C# program to compute sum // of series 1/1! + 1/2! + .. + 1/n! using System; class GFG { // An Efficient Function to return // value of 1/1! + 1/2! + .. + 1/n! static double sum(int n) { double sum = 0; int fact = 1; for (int i = 1; i <= n; i++) { // Update factorial fact *= i; // Update series sum sum += 1.0 / fact; } return sum; } // Driver program public static void Main () { int n = 5; Console.WriteLine(sum(n)); } } // This code is contributed by Sam007.
PHP
<?php // A simple PHP program to // compute sum of series // 1/1! + 1/2! + .. + 1/n! // An Efficient Function to // return value of 1/1! + // 1/2! + .. + 1/n! function sum($n) { $sum = 0; $fact = 1; for ($i = 1; $i <= $n; $i++) { // Update factorial $fact *= $i; // Update series sum $sum += 1.0 / $fact; } return $sum; } // Driver Code $n = 5; echo sum($n); // This code is contributed by vt_m. ?>
Javascript
<script> // A simple Javascript program to compute sum // of series 1/1! + 1/2! + .. + 1/n! // An Efficient Function to return // value of 1/1! + 1/2! + .. + 1/n! function sum(n) { let sum = 0; let fact = 1; for (let i = 1; i <= n; i++) { // Update factorial fact *= i; // Update series sum sum += 1.0 / fact; } return sum.toFixed(5); } let n = 5; document.write(sum(n)); </script>
Producción:
1.71667
Complejidad de tiempo : O (n) desde que se usa un solo ciclo
Espacio auxiliar: O(1)
Este artículo es una contribución de Rahul Gupta . Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA