> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Factorial Trailing Zeroes Python Solution

> Tested Python solution for LeetCode 172 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 172, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/factorial-trailing-zeroes/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 172   # by problem number
lcpy gen -s factorial_trailing_zeroes   # by problem name
```

## Problem

Given an integer \<code>n\</code>, return \<em>the number of trailing zeroes in \</em>\<code>n!\</code>.

\<p>Note that \<code>n! = n \* (n - 1) \* (n - 2) \* ... \* 3 \* 2 \* 1\</code>.\</p>

### Examples

```
Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.
```

```
Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.
```

```
Input: n = 0
Output: 0
```

### Constraints

* 0 \<= n \<= 10^4

\<p>\<strong>Follow up:\</strong> Could you write a solution that works in logarithmic time complexity?\</p>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/factorial_trailing_zeroes/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/factorial_trailing_zeroes/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log n) (base 5)
    # Space: O(1)
    def trailing_zeroes(self, n: int) -> int:
        zero_count = 0
        while n > 0:
            n //= 5
            zero_count += n
        return zero_count
```

## Complexity

| Time | Space |
| - | - |
| O(log n) (base 5) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.