Very cool! I'm a Ruby enthusiast so I tried it out in that.
Your Iterative strategy was great but I think using recursion could help reduce some of the complexity and make for some cleaner code.
def d_root(num) return num if num < 10 d_root(num.to_s.split('').reduce(0) {|acc, digit| acc + digit.to_i}) end
Ruby gives us some great helpers for integers so we can simplify this even more.
def d_root(num) return num if num < 10 d_root(num.digits.sum) end
Nice and simple!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Very cool! I'm a Ruby enthusiast so I tried it out in that.
Your Iterative strategy was great but I think using recursion could help reduce some of the complexity and make for some cleaner code.
Ruby gives us some great helpers for integers so we can simplify this even more.
Nice and simple!