- Published on

# Trailing Zeros in Factorial

- Authors
- Name
- Richard Feng
- @richard____feng

For any positive integer $n$, the factorial of $n$, denoted as $n!$, is defined as

$n!=n\times(n−1)\times(n−2)\times...\times2\times1.$

$0!$ is defined as 1, i.e. $0!=1$, though it looks unintutive. However, we can write this formula recursively as follows:

$f(n)=f(n-1)\times n$

$f(0)=0$

Now, for $n=1$, we have $1!=1\times 0!$ and this implies that $0!=1$.

The table below shows the values of the factorial for the integers 1 to 16.

$n$ | $n!$ |
---|---|

1 | 1 |

2 | 2 |

3 | 6 |

4 | 24 |

5 | 120 |

6 | 720 |

7 | 5040 |

8 | 40320 |

9 | 362880 |

10 | 3628800 |

11 | 39916800 |

12 | 479001600 |

13 | 6227020800 |

14 | 87178291200 |

15 | 1307674368000 |

16 | 20922789888000 |

## Counting the Trailing Zeros

How do we find the number of trailing zeros in a factorial? For example, when $n$ is 5, $n!$ has 1 trailing zero; when $n$ is 12, $n!$ has 2 trailing zeros; when $n$ is 16, $n!$ has 3 trailing zeros.

Given a positive integer number $n$, how many trailing zeroes are in $n$ factorial?

Bonus: can you implement this in any programming language?