I have this code that I'm trying to combine, but I keep getting the stack level too high:
def zeros(n) trailing_zeros(n) if n == 1 zeros(n-1) * n end def trailing_zeros(number) sort_sum = number.to_s.split(//).reverse counter = 0 until sort_sum[counter] != "0" counter += 1 end counter end puts zeros(5)
Individually, they work fine, but when I try to combine them, I have problems and I donβt understand why. Why is it too complicated. From an experienced developer. What would you say that this will be a mistake of this type? I understand that infinite recursions or something with a really large number can cause this, but what is the limit? In addition, I read from wikipedia that these errors also have something to do with the system, that your startup and the amount of memory that it can use or allocate to methods. It's true?
------- EDIT ---------
Well, it doesn't matter if my question is left out, because I really don't understand what I'm doing wrong. I also wanted to mention that I was trying to use return trailing_zeros (n), as you guys mentioned.
def zeros(n) return trailing_zeros(n) if n == 1 zeros(n-1) * n end
The only problem with this is that I get a value of 0. I saw it by inserting binding.pry. I know this is a question about noob, but I just don't understand what is wrong here. Thank you guys for your patience.
------- ------ EDIT
To clarify, I'm trying to get the trailing zeros of a factorial. If I go through 5, I will get 1 # 120. If I go through 12, I will get 2 # 479001600
ruby
Dan rubio
source share