Given an integer n and base B, the task is to find the length of n! in base B.
Input: n = 4, b = 10
Explanation: 4! = 24, hence number of digits is 2
Input: n = 4, b = 16
Explanation: 4! = 18 in base 16, hence number of digits is 2
In order to solve the problem we use Kamenetsky’s formula which approximates the number of digits in a factorial
f(x) = log10( ((n/e)^n) * sqrt(2*pi*n))
The number of digits in n to the base b is given by logb(n) = log10(n) / log10(b). Hence, by using properties of logarithms, the number of digits of factorial in base b can be obtained by
f(x) = ( n* log10(( n/ e)) + log10(2*pi*n)/2 ) / log10(b)
This approach can deal with large inputs that can be accommodated in a 32-bit integer and even beyond that!
Below code is the implementation of above idea :
2 3 8 16
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Given a number N in decimal base, find number of its digits in any base (base b)
- Given a number N in decimal base, find the sum of digits in any base B
- Find the Nth digit from right in base B of the given number in Decimal base
- Find the last digit when factorial of A divides factorial of B
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Add two integers of different base and represent sum in smaller base of the two
- Check if a given number is factorial of any number
- Check if a given number can be represented in given a no. of digits in any base
- Complement of a number with any base b
- Check if a subarray of length K with sum equal to factorial of a number exists or not
- Convert from any base to decimal and vice versa
- Program to compute log a to any base b (logb a)
- Find the last two digits of Factorial of a given Number
- Find GCD of factorial of elements of given array
- Find the first natural number whose factorial is divisible by x
- Find sum of digits in factorial of a number
- Find maximum power of a number that divides a factorial
- Python program to find the factorial of a number using recursion
- Find the total count of numbers up to N digits in a given base B
- Calculate MDAS Factorial of given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.