Given an integer n, 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
A naive solution would be to calculate the n! first and then calculate the number of digits present in it. However as the value for n! can be very large, it would become cumbersome to store them in a variable (Unless you’re working in python!) .
A better solution would be to use the useful property of logarithms to calculate the required answer.
We know, log(a*b) = log(a) + log(b) Therefore log( n! ) = log(1*2*3....... * n) = log(1) + log(2) + ........ +log(n) Now, observe that the floor value of log base 10 increased by 1, of any number, gives the number of digits present in that number. Hence, output would be : floor(log(n!)) + 1.
Below is a the implementation of the same.
1 3 7 199
In the next set, we’d see how to further optimize our approach and reduce the time complexity for the same program.
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 2
- 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 integers in a range which have even number of odd digits and odd number of even 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
- 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
- Smallest number with given sum of digits and sum of square of digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Minimum number of digits to be removed so that no two consecutive digits are same