Ruby factorial function

前端 未结 19 2085
刺人心
刺人心 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 11:03

    And yet another way (=

    def factorial(number)
      number = number.to_i
      number_range = (number).downto(1).to_a
      factorial = number_range.inject(:*)
      puts "The factorial of #{number} is #{factorial}"
    end
    factorial(#number)
    

提交回复
热议问题