the number of trailing zeros in a factorial of a given number - Ruby

后端 未结 12 1898
执念已碎
执念已碎 2020-12-11 03:37

Having a little trouble trying calculate the number of trailing zeros in a factorial of a given number. This is one of the challenges from Codewars- can\'t get mine to pass.

12条回答
  •  醉梦人生
    2020-12-11 04:14

    Here's a solution that is easier to read:

    def zeros(num)
      char_array = num.to_s.split('')
      count = 0
    
      while char_array.pop == "0"
        count += 1
      end
    
      count
    end
    

    Let me know what you think and feel free to edit if you see an improvement!

提交回复
热议问题