# 讲两道常考的阶乘算法题

Info

**已完成网站教程、网站习题、配套插件中所有多语言代码的校准，解决了之前 chatGPT 翻译可能出错的问题~**

读完本文，你不仅学会了算法套路，还可以顺便解决如下题目：

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.