Given an integer n (can be very large), find the number of digits that appear in its factorial, where factorial is defined as, factorial(n) = 1*2*3*4……..*n and factorial(0) = 1
Input : n = 1 Output : 1 1! = 1, hence number of digits is 1 Input : 5 Output : 3 5! = 120, i.e., 3 digits Input : 10 Output : 7 10! = 3628800, i.e., 7 digits Input : 50000000 Output : 363233781 Input : 1000000000 Output : 8565705523
We’ve already discussed the solution for small values of n in the Count digits in a factorial | Set 1. However that solution would not be able to handle cases where n >10^6
So, can we improve our solution ?
Yes ! we can.
We can use Kamenetsky’s formula to find our answer !
It 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
And that’s it !
Our solution can handle very large inputs that can be accommodated in a 32 bit integer,
and even beyond that ! .
Below is the implementation of above idea :
1 363233781 8565705523 199
References : oeis.org
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.
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.
- Find the last digit when factorial of A divides factorial of B
- Count digits in a factorial | Set 1
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Smallest number with at least n digits in factorial
- 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
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count trailing zeroes in factorial of a number
- Count factorial numbers in a given range
- Count Divisors of Factorial
- Golang Program to Count Trailing Zeros in Factorial of a Number
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits