Dado n, ¡necesitamos encontrar la suma de 1*1! + 2*2! + ……..+ n*n!
Ejemplos:
Entrada: 1
Salida: 1
Entrada: 3
Salida: 23
1 * 1! + 2 * 2! + 3 * 3! = 1 + 4 + 18 = 23
Podemos suponer que el desbordamiento no ocurre.
Una solución simple es calcular los términos uno por uno y agregarlos al resultado.
¡ Una solución eficiente se basa en la fórmula directa (n + 1)! – 1
¿Cómo funciona esta fórmula?
Básicamente necesitamos calcular debajo de la suma.
∑(i * i!) Donde i varía de 1 a n
= ∑((i + 1 – 1) * i!)
= ∑((i+1) * i!) – ∑i!
= ∑(yo + 1)! – ∑(yo!)
∑(yo + 1)! = 2! + 3! + … (n+1)! donde 1 <= i <= n —–(1)
∑(i!) = 1! + 2! + 3! +… (n)! donde 1 <= i <= n —–(2)
¡Restando el segundo del primero, obtenemos (n+1)! – 1
C++
// CPP program to find sum of the series. #include <bits/stdc++.h> using namespace std; int factorial(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } // Function to calculate required series int calculateSeries(int n) { return factorial(n + 1) - 1; } // Drivers code int main() { int n = 3; cout << calculateSeries(n); return 0; }
Java
// Java program to find sum of // the series. import java.io.*; class GFG { static int factorial(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } // Function to calculate required // series static int calculateSeries(int n) { return factorial(n + 1) - 1; } // Drivers code public static void main (String[] args) { int n = 3; System.out.println( calculateSeries(n)); } } // This code is contributed by anuj_67.
Python3
# Python program to find sum of # the series. def factorial(n): res = 1 for i in range(2, n+1): res = res * i return res # Function to calculate required # series def calculateSeries(n): return factorial(n + 1) - 1 # Drivers code n = 3 print(calculateSeries(n)) # This code is contributed by # Ansu Kumari.
C#
// C# program to find // sum of the series. using System; class GFG { static int factorial(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } // Function to calculate // required series static int calculateSeries(int n) { return factorial(n + 1) - 1; } // Driver code static public void Main () { int n = 3; Console.WriteLine( calculateSeries(n)); } } // This code is contributed by ajit.
PHP
<?php // PHP program to find // sum of the series. function factorial($n) { $res = 1; for ($i = 2; $i <= $n; $i++) $res = $res * $i; return $res; } // Function to calculate // required series function calculateSeries($n) { return factorial($n + 1) - 1; } // Driver code $n = 3; echo calculateSeries($n); // This code is contributed // by akt_mit ?>
Javascript
<script> // java script program to find // sum of the series. function factorial(n) { let res = 1; for (let i = 2; i <= n; i++) res = res * i; return res; } // Function to calculate // required series function calculateSeries(n) { return factorial(n + 1) - 1; } // Driver code let n = 3; document.write( calculateSeries(n)); // This code is contributed // by sravan kumar </script>
Producción:
23