Un número F es un número factorial si existe algún entero I >= 0 tal que F = I! (es decir, F es factorial de I). Ejemplos de números factoriales son 1, 2, 6, 24, 120,….
Escriba un programa que tome como entrada dos enteros largos ‘bajo’ y ‘alto’ donde 0 < bajo < alto y encuentre el conteo de números factoriales en el intervalo cerrado [bajo, alto].
Ejemplos:
Input: 0 1 Output: 1 //Reason: Only factorial number is 1 Input: 12 122 Output: 2 // Reason: factorial numbers are 24, 120 Input: 2 720 Output: 5 // Factorial numbers are: 2, 6, 24, 120, 720
1) Encuentra el primer factorial que sea mayor o igual a bajo . Sea este factorial x! (factorial de x) y el valor de este factorial sea ‘fact’
2) Siga incrementando x, y siga actualizando ‘fact’ mientras fact es menor o igual que high . Cuente el número de veces que se ejecuta este ciclo.
3) Devuelva el conteo calculado en el paso 2.
A continuación se muestra la implementación del algoritmo anterior. Gracias a Kartik por sugerir la siguiente solución.
C++
// C++ Program to count factorial numbers in given range #include <iostream> using namespace std; int countFact(int low, int high) { // Find the first factorial number 'fact' greater than or // equal to 'low' int fact = 1, x = 1; while (fact < low) { fact = fact*x; x++; } // Count factorial numbers in range [low, high] int res = 0; while (fact <= high) { res++; fact = fact*x; x++; } // Return the count return res; } // Driver program to test above function int main() { cout << "Count is " << countFact(2, 720); return 0; }
Java
// Java Program to count factorial // numbers in given range class GFG { static int countFact(int low, int high) { // Find the first factorial number // 'fact' greater than or equal to 'low' int fact = 1, x = 1; while (fact < low) { fact = fact * x; x++; } // Count factorial numbers // in range [low, high] int res = 0; while (fact <= high) { res++; fact = fact * x; x++; } // Return the count return res; } // Driver code public static void main (String[] args) { System.out.print("Count is " + countFact(2, 720)); } } // This code is contributed by Anant Agarwal.
Python3
# Python3 Program to count factorial # numbers in given range def countFact(low,high): # Find the first factorial number # 'fact' greater than or # equal to 'low' fact = 1 x = 1 while (fact < low): fact = fact * x x += 1 # Count factorial numbers # in range [low, high] res = 0 while (fact <= high): res += 1 fact = fact * x x += 1 # Return the count return res # Driver code print("Count is ", countFact(2, 720)) # This code is contributed # by Anant Agarwal.
C#
// C# Program to count factorial // numbers in given range using System; public class GFG { // Function to count factorial static int countFact(int low, int high) { // Find the first factorial number numbers // 'fact' greater than or equal to 'low' int fact = 1, x = 1; while (fact < low) { fact = fact * x; x++; } // Count factorial numbers // in range [low, high] int res = 0; while (fact <= high) { res++; fact = fact * x; x++; } // Return the count return res; } // Driver code public static void Main () { Console.Write("Count is " + countFact(2, 720)); } } // This code is contributed by Sam007
PHP
<?php // PHP Program to count factorial // numbers in given range function countFact($low, $high) { // Find the first factorial // number 'fact' greater // than or equal to 'low' $fact = 1; $x = 1; while ($fact < $low) { $fact = $fact * $x; $x++; } // Count factorial numbers // in range [low, high] $res = 0; while ($fact <= $high) { $res++; $fact = $fact * $x; $x++; } // Return the count return $res; } // Driver Code echo "Count is " , countFact(2, 720); // This code is contributed by ajit ?>
Javascript
<script> // Javascript Program to count factorial // numbers in given range function countFact(low, high) { // Find the first factorial // number 'fact' greater // than or equal to 'low' let fact = 1; let x = 1; while (fact < low) { fact = fact * x; x++; } // Count factorial numbers // in range [low, high] let res = 0; while (fact <= high) { res++; fact = fact * x; x++; } // Return the count return res; } // Driver Code document.write("Count is " + countFact(2, 720)); // This code is contributed by _saurabh_jaiswal </script>
Producción :
Count is 5
Complejidad temporal: aproximadamente igual a O(K) donde K es el mayor devisor de alto y no es igual a alto.
Complejidad del espacio auxiliar: O(1).
Este artículo es una contribución de Shivam. 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