Ruby factorial function

前端 未结 19 2134
刺人心
刺人心 2020-12-02 10:31

I\'m going crazy: Where is the Ruby function for factorial? No, I don\'t need tutorial implementations, I just want the function from the library. It\'s not in Math!

19条回答
  •  感动是毒
    2020-12-02 10:58

    Here is my version seems to be clear to me even though it's not as clean.

    def factorial(num)
        step = 0
        (num - 1).times do (step += 1 ;num *= step) end
        return num
    end
    

    This was my irb testing line that showed each step.

    num = 8;step = 0;(num - 1).times do (step += 1 ;num *= step; puts num) end;num
    

提交回复
热议问题