Dado un entero n y base B , ¡la tarea es encontrar la longitud de n! en la base B.
Ejemplos:
Entrada: n = 4, b = 10
Salida: 2
Explicación: 4! = 24, por lo que el número de dígitos es 2
Entrada: n = 4, b = 16
Salida: 2
Explicación: ¡4! = 18 en base 16, por lo tanto, el número de dígitos es 2
Enfoque:
Para resolver el problema usamos la fórmula de Kamenetsky que aproxima el número de dígitos en un factorial
f(x) = log10( ((n/e)^n) * sqrt(2*pi*n))
El número de dígitos en n a la base b está dado por logb(n) = log10(n) / log10(b) . Por lo tanto, usando las propiedades de los logaritmos, el número de dígitos del factorial en base b se puede obtener por
f(x) = ( n* log10(( n/ e)) + log10(2*pi*n)/2 ) / log10(b)
Este enfoque puede manejar grandes entradas que se pueden acomodar en un número entero de 32 bits e incluso más.
El siguiente código es la implementación de la idea anterior:
C++
// A optimised program to find the // number of digits in a factorial in base b #include <bits/stdc++.h> using namespace std; // Returns the number of digits present // in n! in base b Since the result can be large // long long is used as return type long long findDigits(int n, int b) { // factorial of -ve number // doesn't exists if (n < 0) return 0; // base case if (n <= 1) return 1; // Use Kamenetsky formula to calculate // the number of digits double x = ((n * log10(n / M_E) + log10(2 * M_PI * n) / 2.0)) / (log10(b)); return floor(x) + 1; } // Driver Code int main() { //calling findDigits(Number, Base) cout << findDigits(4, 16) << endl; cout << findDigits(5, 8) << endl; cout << findDigits(12, 16) << endl; cout << findDigits(19, 13) << endl; return 0; }
Java
// A optimised program to find the // number of digits in a factorial in base b class GFG{ // Returns the number of digits present // in n! in base b Since the result can be large // long is used as return type static long findDigits(int n, int b) { // factorial of -ve number // doesn't exists if (n < 0) return 0; // base case if (n <= 1) return 1; double M_PI = 3.141592; double M_E = 2.7182; // Use Kamenetsky formula to calculate // the number of digits double x = ((n * Math.log10(n / M_E) + Math.log10(2 * M_PI * n) / 2.0)) / (Math.log10(b)); return (long) (Math.floor(x) + 1); } // Driver Code public static void main(String[] args) { //calling findDigits(Number, Base) System.out.print(findDigits(4, 16) +"\n"); System.out.print(findDigits(5, 8) +"\n"); System.out.print(findDigits(12, 16) +"\n"); System.out.print(findDigits(19, 13) +"\n"); } } // This code is contributed by 29AjayKumar
Python 3
from math import log10,floor # A optimised program to find the # number of digits in a factorial in base b # Returns the number of digits present # in n! in base b Since the result can be large # long long is used as return type def findDigits(n, b): # factorial of -ve number # doesn't exists if (n < 0): return 0 M_PI = 3.141592 M_E = 2.7182 # base case if (n <= 1): return 1 # Use Kamenetsky formula to calculate # the number of digits x = ((n * log10(n / M_E) + log10(2 * M_PI * n) / 2.0)) / (log10(b)) return floor(x) + 1 # Driver Code if __name__ == '__main__': #calling findDigits(Number, Base) print(findDigits(4, 16)) print(findDigits(5, 8)) print(findDigits(12, 16)) print(findDigits(19, 13)) # This code is contributed by Surendra_Gangwar
C#
// A optimised C# program to find the // number of digits in a factorial in base b using System; class GFG{ // Returns the number of digits present // in n! in base b Since the result can be large // long is used as return type static long findDigits(int n, int b) { // factorial of -ve number // doesn't exists if (n < 0) return 0; // base case if (n <= 1) return 1; double M_PI = 3.141592; double M_E = 2.7182; // Use Kamenetsky formula to calculate // the number of digits double x = ((n * Math.Log10(n / M_E) + Math.Log10(2 * M_PI * n) / 2.0)) / (Math.Log10(b)); return (long) (Math.Floor(x) + 1); } // Driver Code public static void Main(string[] args) { // calling findDigits(Number, Base) Console.WriteLine(findDigits(4, 16)); Console.WriteLine(findDigits(5, 8)); Console.WriteLine(findDigits(12, 16)); Console.WriteLine(findDigits(19, 13)); } } // This code is contributed by Yash_R
Javascript
<script> // A optimised program to find the // number of digits in a factorial in base b // Returns the number of digits present // in n! in base b Since the result can be large // long long is used as return type function findDigits(n, b) { // factorial of -ve number // doesn't exists if (n < 0) return 0; // base case if (n <= 1) return 1; var M_PI = 3.141592; var M_E = 2.7182; // Use Kamenetsky formula to calculate // the number of digits var x = ((n * Math.log10(n / M_E) + Math.log10(2 * M_PI * n) / 2.0)) / (Math.log10(b)); return Math.floor(x) + 1; } // Driver Code // calling findDigits(Number, Base) document.write(findDigits(4, 16) + "<br>"); document.write( findDigits(5, 8) + "<br>"); document.write( findDigits(12, 16) + "<br>"); document.write( findDigits(19, 13) + "<br>"); // This code is contributed by rutvik_56. </script>
2 3 8 16
Referencia: oeis.org