Ruby factorial function

前端 未结 19 1984
刺人心
刺人心 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:38

    In math, factorial of n is just the gamma function of n+1
    (see: http://en.wikipedia.org/wiki/Gamma_function)

    Ruby has Math.gamma() so just use Math.gamma(n+1) and cast it back to an integer if desired.

    0 讨论(0)
  • 2020-12-02 10:40

    Shamelessly cribbed from http://rosettacode.org/wiki/Factorial#Ruby, my personal favorite is

    class Integer
      def fact
        (1..self).reduce(:*) || 1
      end
    end
    
    >> 400.fact
    => 64034522846623895262347970319503005850702583026002959458684445942802397169186831436278478647463264676294350575035856810848298162883517435228961988646802997937341654150838162426461942352307046244325015114448670890662773914918117331955996440709549671345290477020322434911210797593280795101545372667251627877890009349763765710326350331533965349868386831339352024373788157786791506311858702618270169819740062983025308591298346162272304558339520759611505302236086810433297255194852674432232438669948422404232599805551610635942376961399231917134063858996537970147827206606320217379472010321356624613809077942304597360699567595836096158715129913822286578579549361617654480453222007825818400848436415591229454275384803558374518022675900061399560145595206127211192918105032491008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    

    This implementation also happens to be the fastest among the variants listed in Rosetta Code.

    update #1

    Added || 1 to handle the zero case.

    update #2

    With thanks and appreciation to Mark Thomas, here's a version that is a bit more efficient, elegant and obscure:

    class Integer
      def fact
        (2..self).reduce(1,:*)
      end
    end
    
    0 讨论(0)
  • 2020-12-02 10:40

    Why would the standard library require a factorial method, when there is a built-in iterator for this exact purpose? It is called upto.

    No you do not need to use recursion, like all these other answers show.

    def fact(n)
      n == 0 ? 1 : n * fact(n - 1)
    end  
    

    Rather, the built-in iterator upto can be used to calculate factorials:

    factorial = 1
    1.upto(10) {|x| factorial *= x }
    factorial
     => 3628800
    
    0 讨论(0)
  • 2020-12-02 10:41

    Just call this function

    def factorial(n=0)
      (1..n).inject(:*)
    end
    

    examples

    factorial(3)
    factorial(11)
    
    0 讨论(0)
  • 2020-12-02 10:45

    It's not in the standard library but you can extend the Integer class.

    class Integer
      def factorial_recursive
        self <= 1 ? 1 : self * (self - 1).factorial
      end
      def factorial_iterative
        f = 1; for i in 1..self; f *= i; end; f
      end
      alias :factorial :factorial_iterative
    end
    

    N.B. Iterative factorial is a better choice for obvious performance reasons.

    0 讨论(0)
  • 2020-12-02 10:47
    class Integer
      def factorial
        return self < 0 ? false : self==0 ? 1 : self.downto(1).inject(:*)
        #Not sure what other libraries say, but my understanding is that factorial of 
        #anything less than 0 does not exist.
      end
    end
    
    0 讨论(0)
提交回复
热议问题