Dado un número N (≤ 10 18 ), la tarea es encontrar el siguiente número factorial mayor que N .
Ejemplos:
Entrada: N = 24
Salida: 120
Explicación:
¡Como 4! = 24. Entonces, el siguiente número factorial y mayor que 24 es 5!, que es 120
Entrada: N = 150
Salida: 720
Explicación:
¡Como 5! = 120. Así que el siguiente número factorial y mayor que 150 es 6!, que es 720.
Acercarse:
- ¡Precalcule el factorial de todos los números hasta 20 en una array como 20! > 10 18 .
- Atraviese la array factorial y encuentre el valor que es justo mayor que N como el siguiente número factorial requerido.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include "bits/stdc++.h" using namespace std; // Array that stores the factorial // till 20 long long fact[21]; // Function to pre-compute // the factorial till 20 void preCompute() { // Precomputing factorials fact[0] = 1; for (int i = 1; i < 18; i++) fact[i] = (fact[i - 1] * i); } // Function to return the next // factorial number greater than N void nextFactorial(int N) { // Traverse the factorial array for (int i = 0; i < 21; i++) { // Find the next just greater // factorial than N if (N < fact[i]) { cout << fact[i]; break; } } } // Driver Code int main() { // Function to precalculate // the factorial till 20 preCompute(); int N = 120; // Function call nextFactorial(N); return 0; }
Java
// Java implementation of the above approach class GFG { // Array that stores the factorial // till 20 final static int fact[] = new int[21]; // Function to pre-compute // the factorial till 20 static void preCompute() { // Precomputing factorials fact[0] = 1; for (int i = 1; i < 18; i++) fact[i] = (fact[i - 1] * i); } // Function to return the next // factorial number greater than N static void nextFactorial(int N) { // Traverse the factorial array for (int i = 0; i < 21; i++) { // Find the next just greater // factorial than N if (N < fact[i]) { System.out.println(fact[i]); break; } } } // Driver Code public static void main (String[] args) { // Function to precalculate // the factorial till 20 preCompute(); int N = 120; // Function call nextFactorial(N); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach # Array that stores the factorial # till 20 fact = [0] * 21 # Function to pre-compute # the factorial till 20 def preCompute(): # Precomputing factorials fact[0] = 1 for i in range(1, 18): fact[i] = (fact[i - 1] * i) # Function to return the next # factorial number greater than N def nextFactorial(N): # Traverse the factorial array for i in range(21): # Find the next just greater # factorial than N if N < fact[i]: print(fact[i]) break # Driver Code # Function to precalculate # the factorial till 20 preCompute() N = 120 # Function call nextFactorial(N) # This code is contributed by divyamohan123
C#
// C# implementation of the above approach using System; class GFG { // Array that stores the factorial // till 20 static int []fact = new int[21]; // Function to pre-compute // the factorial till 20 static void preCompute() { // Precomputing factorials fact[0] = 1; for (int i = 1; i < 18; i++) fact[i] = (fact[i - 1] * i); } // Function to return the next // factorial number greater than N static void nextFactorial(int N) { // Traverse the factorial array for (int i = 0; i < 21; i++) { // Find the next just greater // factorial than N if (N < fact[i]) { Console.WriteLine(fact[i]); break; } } } // Driver Code public static void Main (string[] args) { // Function to precalculate // the factorial till 20 preCompute(); int N = 120; // Function call nextFactorial(N); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the above approach // Array that stores the factorial // till 20 fact = Array(21).fill(0); // Function to pre-compute // the factorial till 20 function preCompute() { // Precomputing factorials fact[0] = 1; for (var i = 1; i < 18; i++) fact[i] = (fact[i - 1] * i); } // Function to return the next // factorial number greater than N function nextFactorial(N) { // Traverse the factorial array for (var i = 0; i < 21; i++) { // Find the next just greater // factorial than N if (N < fact[i]) { document.write(fact[i]); break; } } } // Driver Code // Function to precalculate // the factorial till 20 preCompute(); var N = 120; // Function call nextFactorial(N); // This code is contributed by rutvik_56. </script>
Producción:
720
Complejidad del tiempo: O(21)
Espacio Auxiliar: O(21)