Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.

30:00

Factorial Trailing Zeroes
medium
Free written solution
Topics
Companies

Given an integer n, return the number of trailing zeroes in n!. Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.

Example 1:
Input: {"n":3}
Output: 0
Constraints:
  • 0≤n≤1040 \leq n \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":3}

3! = 1 × 2 × ... × 3. Each trailing zero is a factor of 10 = 2 × 5, and factors of 2 are far more common than factors of 5, so the answer is how many 5s divide 3!.

n (now 3 // 5^0)

3

count (factors of 5)

0

Factors of 3!: each dot is one factor of 5 inside that number

1

2

3

Multiples of each power of 5 up to 3

÷5

?

count = 0

3! written out

6

Variables
VariableValue
n3
count0
DepthFunction Call
Stack empty
0/6