Given a number n. The task is to find the smallest number whose factorial contains at least n digits.
Input : n = 1 Output : 0 0! = 1, hence it has 1 digit. Input : n = 2 Output : 4 4! = 24 and 3! = 6, hence 4 is the smallest number having 2 digits in its factorial Input : n = 5 Output : 8
In the article for Count digits in a factorial of a number, we have discussed how we can efficiently find the number of digits in factorial.
We used the below formula to find the number of digits
Kamenetsky’s formula approximates the number of digits in a factorial by : f(x) = log10(((n/e)n) * sqrt(2*pi*n)) Thus, we can pretty easily use the property of logarithms to , f(x) = n*log10((n/e)) + log10(2*pi*n)/2
Now we need to determine the interval in which we can find a factorial which has at least n digits. Following are some observations:
- For a large number we can always say that it’s factorial has more digits than the number itself. For example factorial of 100 has 158 digits which is greater than 100.
- However for smaller numbers, that might not be the case. For example factorial of 8 has only 5 digits, which is less than 8. In fact numbers up to 21 follow this trend.
Hence if we search from 0! to n! to find a result having at least n digits, we won’t be able to find the result for smaller numbers.
For example suppose n = 5, now as we’re searching in [0,n] the maximum number of digits we can obtain is 3, (found in 5! = 120). However if we search in [0, 2*n] (0 to 10), we can find 8! has 5 digits.
Hence, if we can search for all factorial from 0 to 2*n , there will always be a number k which will have at least n digits in its factorial.(Readers are advised to try on their own to figure out this fact)
We can say conclude if we have to find a number k, such that k! has at least n digits, we can be sure that k lies in [0,2*n] i.e., 0<= k <= 2*n
Thus we can do a binary search between 0 to 2*n to find the smallest number having at least n digits.
0 4 8 24 70 532
The complexity for the binary search is O(log(2*n)), if we ignore the complexity of the logarithmic function. Hence overall complexity is O(log(n)).
This article is contributed by Ashutosh Kumar 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Smallest number with at least n trailing zeroes in factorial
- Find the last digit when factorial of A divides factorial of B
- Smallest number S such that N is a factor of S factorial or S!
- Find sum of digits in factorial of a number
- Number of digits in N factorial to the power N
- Find the last two digits of Factorial of a given Number
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count digits in a factorial | Set 2
- Count digits in a factorial | Set 1
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Check if a given number is factorial of any number
- Count trailing zeroes in factorial of a number
- Factorial of a large number
- Find the first natural number whose factorial is divisible by x
- Program for factorial of a number
- Sum of divisors of factorial of a number
- One line function for factorial of a number
- Find maximum power of a number that divides a factorial
- C Program for factorial of a number
- Java Program for factorial of a number