Factorial Trailing Zeroes in JavaA number n is given. Our task is to find the total number of trailing zeros that are present in the value of the factorial of n. See the following examples for a better understanding. Example: 1 Input: int n = 6 Output: 1 Explanation: The factorial of the number 6 is 6! = 6 x 5 x 4 x 3 x 2 x 1 = 720. The number of the trailing zero in the number 720 is 1. Example: 2 Input: int n = 10 Output: 2 Explanation: The factorial of the number 10 is 10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800. The number of the trailing zero in the number 3628800 is 2. Example: 3 Input: int n = 20 Output: 4 Explanation: The factorial of the number 20 is 20! = 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 2432902008176640000. The number of the trailing zero in the number 2432902008176640000 is 4. Naïve ApproachIn this approach, we will compute the value of n! and then will find out the number of trailing zeros in the n!. The illustration of the same is mentioned below. FileName: TrailingZeros.java Output: The factorial of the number 5 is: 120 The number of trailing zeros in the number 120 is: 1 The factorial of the number 10 is: 3628800 The number of trailing zeros in the number 3628800 is: 2 The factorial of the number 20 is: 2432902008176640000 The number of trailing zeros in the number 2432902008176640000 is: 4 Complexity Analysis: Since the program is using a forloop for computing the factorial of the number n, the time complexity of the program is O(n), where n is the input number. The space complexity of the program is constant, i.e., O(1).
Another Approach: Without Computing the FactorialLet's take an example to understand when a number has trailing zeros. Suppose we take the number 3. We see that 3 has no trailing zero. Now multiply the number 3 with 10. We get 3 x 10 = 30, where the number of trailing zero is 1. Observe that 10 has also one trailing zero. Again, multiply the number 3 with 100. We get 3 x 100 = 300, where the number of trailing zero is 2. Note that 100 also has 2 trailing zeros. Thus, we see that number of trailing zero in a number is dependent on the multiple of 10. 100 is a multiple of 2 tens. 10 x 10 = 100, hence 2 trailing zeros. 1000 is the multiple of three tens, 10 x 10 x 10 = 1000, hence 3 trailing zeros. Hence, all we need to do is to find out how many tens are there in the factorial of the given number n. Now the question arises, how do we get 10? 10 is nothing but the multiple of 2 and 5. Therefore, we need to find how many pairs of 2 and 5 are present in the factorial of the number n, which eventually boils down to the number of fives present in the factorial of the number n. It is because the number of fives in the factorial of the number n is always less than the number of tows present in the factorial of the number n. For example: from 1 to 8, there is only one 5 and multiple tows. 1 has zero twos and zero fives. (1 x 1 = 1) 2 has 1 two and zero fives. (2 x 1 = 2) 3 has zero twos and zero fives. (3 x 1 = 3) 4 has 2 twos and zero fives. (2 x 2 = 4) 5 has zero twos and 1 five. (5 x 1 = 5) 6 has 1 two and zero fives. (2 x 3 = 6). 7 has zero twos and zero fives. (7 x 1 = 7) 8 has 3 twos and zero fives. (2 x 2 x 2 = 8) Thus, from 1 to 8 we have (1 + 2 + 1 + 3) 7 twos and one five. Thus, we see that number of fives is less than the number of twos. Thus, we only get one pair as there is only one five. Thus, the number of trailing zeros in the 8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320, and the number 40320 has only one trailing zero. Hence, our answer is correct. Now, let's see the implementation of the same. FileName: TrailingZeros1.java Output: The factorial of the number 5 has: 1 trailing zeros. The factorial of the number 10 has: 2 trailing zeros. The factorial of the number 50 has: 12 trailing zeros. Complexity Analysis: The time, as well as space complexity of the program is the same as the previous program. Since the abovewritten program is not computing the factorial of a number, it is efficient enough to find the trailing zeros even for large numbers. A better way to write the above program is mentioned below. The following program is more optimized than the previous one. The Most Optimized ApproachIt is evident from the previous program that if we find the count of the number 5 in the input number, we can find the number of trailing zeros. Thus, all we need to do is to apply the loop and reduce the number by dividing it by 5. The illustration of the same is given below. FileName: TrailingZeros2.java Output: The factorial of the number 5 has: 1 trailing zeros. The factorial of the number 10 has: 2 trailing zeros. The factorial of the number 50 has: 12 trailing zeros. Complexity Analysis: The time complexity of the program is O(log_{5}(n)), where n is the input number. We get this time complexity because we are reducing the number num by dividing it by 5. The space complexity of the program is the same as the previous program.
Next TopicJava Assert Examples
