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 email@example.com. 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.
- Smallest number S such that N is a factor of S factorial or S!
- Smallest number with at least n trailing zeroes in factorial
- Find sum of digits in factorial of a number
- Number of digits in N factorial to the power N
- Smallest even digits number not less than N
- Smallest Even number with N digits
- Smallest odd number with N digits
- Smallest odd digits number not less than N
- Smallest number with sum of digits as N and divisible by 10^N
- Find the smallest number whose digits multiply to a given number n
- Smallest number k such that the product of digits of k is equal to n
- Smallest number by rearranging digits of a given number
- Immediate smallest number after re-arranging the digits of a given number
- Count digits in a factorial | Set 1
- Count digits in a factorial | Set 2