- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0172-factorial-trailing-zeroes.rb
47 lines (37 loc) · 883 Bytes
/
0172-factorial-trailing-zeroes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
# 172. Factorial Trailing Zeroes
# https://leetcode.com/problems/factorial-trailing-zeroes
# Medium
=begin
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
Explanation: 3! = 6, no trailing zero.
Example 2:
Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Example 3:
Input: n = 0
Output: 0
Constraints:
0 <= n <= 104
=end
# @param {Integer} n
# @return {Integer}
deftrailing_zeroes(n)
(n / 5).zero? ? 0 : n / 5 + trailing_zeroes(n / 5)
end
# ********************#
# TEST #
# ********************#
require"test/unit"
classTest_trailing_zeroes < Test::Unit::TestCase
deftest_
assert_equal0,trailing_zeroes(3)
assert_equal1,trailing_zeroes(5)
assert_equal0,trailing_zeroes(0)
end
end