Ruby因子函数

问题描述 投票:84回答:19

我疯了:因子的Ruby函数在哪里?不,我不需要教程实现,我只想要库中的函数。这不是数学!

我开始怀疑,这是一个标准的库函数吗?

ruby math factorial
19个回答
131
投票

标准库中没有因子函数。


3
投票

使用(1..n).inject(1, :*) 是一种简单的方法来生成近似值,然后将其向后舍入到正确的整数结果。应该适用于所有整数,如有必要,请包括输入检查。


3
投票

Just call this function

def fact(n)
  if n<= 1
    1
  else
    n * fact( n - 1 )
  end
end

examples

def fall_fact(n,k)
  if k <= 0
    1
  else
    n*fall_fact(n - 1, k - 1)
  end
end

1
投票

只是另一种方法,虽然它没有必要。

Math.gamma.floor

1
投票

您可能会发现Ruby def factorial(n=0) (1..n).inject(:*) end 很有用。它包含一个非常重要的factorial(3) factorial(11) ,其中包括class Factorial attr_reader :num def initialize(num) @num = num end def find_factorial (1..num).inject(:*) || 1 end end number = Factorial.new(8).find_factorial puts number 。原始循环与批次中呈现的解决方案之间的速度差异可以是100倍(百倍)。全部用纯Ruby编写。


0
投票

0
投票

而另一种方式(=

patch

0
投票

这是我的版本似乎很清楚,即使它不是那么干净。

demo Bash script

这是我的irb测试线,显示了每一步。

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
投票

还有一种方法:

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)

0
投票

当存在用于此确切目的的内置迭代器时,为什么标准库需要阶乘方法?它被称为def factorial(num) step = 0 (num - 1).times do (step += 1 ;num *= step) end return num end

不,你不需要使用递归,就像所有这些其他答案所示。

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

相反,内置迭代器可用于计算阶乘:

# fact(n) => Computes the Factorial of "n" = n!

def fact(n) (1..n).inject(1) {|r,i| r*i }end

fact(6) => 720

0
投票

尊重所有参与并花时间帮助我们的人,我想分享我在这里列出的解决方案的基准。 PARAMS:

迭代= 1000

n = 6

upto

对于n = 10

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

101
投票

喜欢这样比较好

(1..n).inject(:*) || 1

77
投票

它不在标准库中,但您可以扩展Integer类。

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

注:出于明显的性能原因,迭代因子是更好的选择。


22
投票

http://rosettacode.org/wiki/Factorial#Ruby无耻地抄袭,我个人最喜欢的是

class Integer
  def fact
    (1..self).reduce(:*) || 1
  end
end

>> 400.fact
=> 64034522846623895262347970319503005850702583026002959458684445942802397169186831436278478647463264676294350575035856810848298162883517435228961988646802997937341654150838162426461942352307046244325015114448670890662773914918117331955996440709549671345290477020322434911210797593280795101545372667251627877890009349763765710326350331533965349868386831339352024373788157786791506311858702618270169819740062983025308591298346162272304558339520759611505302236086810433297255194852674432232438669948422404232599805551610635942376961399231917134063858996537970147827206606320217379472010321356624613809077942304597360699567595836096158715129913822286578579549361617654480453222007825818400848436415591229454275384803558374518022675900061399560145595206127211192918105032491008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

此实现也恰好是Rosetta Code中列出的变体中最快的。

更新#1

添加了|| 1来处理零情况。

更新#2

感谢和感谢Mark Thomas,这是一个更高效,优雅和模糊的版本:

class Integer
  def fact
    (2..self).reduce(1,:*)
  end
end

13
投票

您还可以使用Math.gamma函数,该函数可归结为整数参数的阶乘。


12
投票

在数学方面,qazxsw poi就是qazxsw poi (见:qazxsw poi)

Ruby有factorial of n所以如果需要,只需使用gamma function of n+1并将其强制转换为整数。


12
投票

8
投票

我会做

Math.gamma(n+1)

6
投票

我刚写了自己的:

class Integer
  def !
    (1..self).inject(:*)
  end
end

此外,您可以定义下降阶乘:

!3  # => 6
!4  # => 24
© www.soinside.com 2019 - 2024. All rights reserved.