Digit Count in a Factorial Of a Number in Java10 Sept 2024 | 6 min read A number n is given to us. Our task is to find the total count of digits present in the number n!. Example 1: Input int n = 9 Output: 6 Explanation: The value of 9! is 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880. The total number of digits present in the number 362880 is 6. Hence, the outcome is 6. Example 2: int n = 12 Output: 479001600 Explanation: The value of 12! is 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600. The total number of digits present in the number 479001600 is 9. Hence, the outcome is 9. Naïve Approach:In the naïve approach, we will first compute the factorial of the given number, and after that, we will find the number of digits present in the factorial of the given number. The illustration of the same is mentioned in the following program. FileName: CntFactDig.java Output: The factorial of the number 9 has 6 digits. The factorial of the number 12 has 9 digits. Complexity Analysis: The program uses a for loop and a while loop. The for loop takes O(N) time, and the while loop takes O(log(N)) time. Therefore, the time complexity of the program is O(N + log(N)), where N is the number whose factorial is to be found. The above approach works well. However, for the larger inputs, it is not possible for the above approach to work properly. For n = 50, 50! = 50 x 49 x 48 x 47 x 46 x … x 45 x 44 … x 3 x 2 x 1 = 30414093201713378043612608166064768844377641568960512000000000000 and storing such a huge number in an int variable is not possible. One may argue that instead of int use long. However, long also has a limit, and that limit can also be exceeded when the factorial of a large number is computed. Thus, we see that no matter what kind of data type we use, the limit of the data type can easily be exceeded when the factorial of a large number is computed. Therefore, it is evident that computing the factorial of the number (in order to compute its digits) is not a foolproof approach. Hence, we need to find a way where computing the factorial of the number is not required. Approach: Using Properties Of LogarithmWe know that, log(p * q) = log(p) + log(q), Thus, log(q!) = log(1 x 2 x 3 x 4 x … q) = log(1) + log(2) + log(3) + log(4) + … + log(q) Now, we see that (log10 of number k) + 1 is the total number of digits present in the number k. We will use this concept to find the total number of digits present in n! in the following program. FileName: CntFactDig1.java Output: The factorial of the number 9 has 6 digits. The factorial of the number 12 has 9 digits. Complexity Analysis: The program uses only one loop. Thus, the time complexity of the program is O(n), where n is the number whose factorial is to be found. The space complexity of the program is constant, i.e., O(1). Approach: Kamenetsky's FormulaThe straight forward approach is to use the Kamenetsky's formula for computing the number of digits present in the factorial of a number. The formula is: P(n) = log10(((n / e) ^ n) * sqrt(2 * Pi * n)), where e = 2.71828182845904523536, and Pi = 3.141592654, and n is the number that will be given to the formula. One of the great advantages of using this formula is that this formula works well for large factorial values. FileName: CntFactDig2.java Output: The factorial of the number 9 has 6 digits. The factorial of the number 12 has 9 digits. The factorial of the number 50 has 65 digits. Complexity Analysis: The time complexity of the program is constant. The space complexity of the program is also constant. Note: We have assumed that Math.log10( ) takes O(1) time.Next TopicValid Parentheses Problem in Java |
Next Greater Number with Same Set of Digits in Java
Greater Number with Same Set of Digits in Java A number (num) is given. The task is to find a number that is the smallest that comprises the same set of digits as num and must be larger than the number num. If the number num...
8 min read
How to Remove substring from String in Java
? In Java, removing a substring from a string involves manipulating the original string to exclude the specified substring. This process can be achieved by various means, typically involving string handling methods that identify the position of the substring and then create a new string without the...
10 min read
Implicitly Typecasting in Java
The process of converting one type of object and variable into another type is referred to as Typecasting. When the conversion automatically performs by the compiler without the programmer's interference, it is called implicit type casting or widening casting. In implicit typecasting, the conversion involves a smaller...
3 min read
Private Constructor in Java
In Java, the constructor is a special type of method that has the same name as the class name. Internally, a constructor is always called when we create an object of the class. It is used to initialize the state of an object. In the same way,...
2 min read
Blockchain Java
Blockchain is a budding technology that has tremendous scope in the coming years. In this tutorial, we will briefly cover the basic concepts of Blockchain. We'll also create a basic Blockchain program in Java to understand how it works in the programming world. What is Blockchain? Blockchain is...
8 min read
Sum of Numbers with Units Digit K in Java
The task of finding numbers whose units digit is equal to k and their sum is equal to the given num is an interesting computational problem which can be solved using different approaches in Java. Example 1 Input num = 58, k = 9 Output: 2...
7 min read
Factorial of a Large Number in Java
We have already discussed the factorial of a number. However, we still have to discuss the factorial of a large number separately. It is because one cannot compute the factorial of a large number with the approach that is used to compute the factorial of a...
8 min read
Ganesha's Pattern in Java
In this section, we will learn about how we can write the code for Lord by using stars or any other special characters. It is one of the most difficult pattern programs to code in Java. We will use the 'for' loop to print the Lord...
2 min read
Rotate A Matrix By 180 Degree in Java
In this section, we will discuss how to rotate a matrix by 180 degree in Java. In this problem, a square matrix is given, and we have to rotate it by 1800. Example 1: Input: 4 6 7 8 9 3 2 1 9 0 4 5 8 0 3 2 Output: 2 3...
10 min read
Non-Generic Vs. Generic Collections in Java
Through Java collections, developers gain a powerful toolset that helps them efficiently maintain and operate object groups in their Java programming environment. Java collections function as non-generic and generic collections, respectively. The addition of generics in Java 5 provided collections with a major advancement that improved both...
5 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India