Two Classic Factorial Problems on LeetCode
Note
Now all the plugins has supported English. I'm still improving the website...
This article will resolve
LeetCode | Difficulty |
---|---|
172. Factorial Trailing Zeroes | 🟠 |
793. Preimage Size of Factorial Zeroes Function | 🔴 |
You often encounter factorial-related questions in written exams. Today, we'll discuss two common questions:
1. Given a non-negative integer n
, calculate how many trailing zeros are in the factorial result n!
.
This is also LeetCode problem #172 "Factorial Trailing Zeroes." For example, if the input is n = 5
, the algorithm returns 1 because 5! = 120
, which has one trailing zero.
The function signature is:
int trailingZeroes(int n);
2. Given a non-negative integer K
, calculate how many integers n
exist such that the factorial result n!
has exactly K
trailing zeros.
This is also LeetCode problem #793 "Preimage Size of Factorial Zeroes Function." For example, if the input is K = 1
, the algorithm returns 5 because the factorials 5!, 6!, 7!, 8!, 9!
each have exactly one trailing zero, meaning there are 5 values of n
that satisfy the condition.
The function signature is:
int preimageSizeFZF(int K);
I've grouped these two problems together because they share common characteristics. Let's analyze them one by one.