Ruby 阶乘函数

发布于 2024-08-24 18:12:25 字数 77 浏览 6 评论 0原文

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

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

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!

I'm starting to doubt, is it a standard library function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

羁客 2024-08-31 18:12:25

标准库中没有阶乘函数。

There is no factorial function in the standard library.

私野 2024-08-31 18:12:25

像这样就更好了

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

Like this is better

(1..n).inject(:*) || 1
月牙弯弯 2024-08-31 18:12:25

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

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

NB 出于明显的性能原因,迭代阶乘是更好的选择。

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_recursive
  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.

嘦怹 2024-08-31 18:12:25

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

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

>> 400.fact
=> 64034522846623895262347970319503005850702583026002959458684445942802397169186831436278478647463264676294350575035856810848298162883517435228961988646802997937341654150838162426461942352307046244325015114448670890662773914918117331955996440709549671345290477020322434911210797593280795101545372667251627877890009349763765710326350331533965349868386831339352024373788157786791506311858702618270169819740062983025308591298346162272304558339520759611505302236086810433297255194852674432232438669948422404232599805551610635942376961399231917134063858996537970147827206606320217379472010321356624613809077942304597360699567595836096158715129913822286578579549361617654480453222007825818400848436415591229454275384803558374518022675900061399560145595206127211192918105032491008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

该实现也恰好是 Rosetta 代码中列出的变体中最快的。

更新 #1

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

更新 #2

感谢和赞赏 Mark Thomas,这里有一个更高效、更优雅、更晦涩的版本:

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

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
梦里°也失望 2024-08-31 18:12:25

在数学中,n 的阶乘 只是 n+1 的 gamma 函数
(参见:http://en.wikipedia.org/wiki/Gamma_function

Math.gamma() 因此只需使用 Math.gamma(n+1) 并将其转换回整数(如果需要)。

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.

心如荒岛 2024-08-31 18:12:25

You could also use Math.gamma function which boils down to factorial for integer parameters.

翻身的咸鱼 2024-08-31 18:12:25
class Integer
  def !
    (1..self).inject(:*)
  end
end

例子

!3  # => 6
!4  # => 24
class Integer
  def !
    (1..self).inject(:*)
  end
end

examples

!3  # => 6
!4  # => 24
记忆里有你的影子 2024-08-31 18:12:25

我会做

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

I would do

(1..n).inject(1, :*)
梦里兽 2024-08-31 18:12:25

我刚刚写了我自己的:

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

另外,你可以定义一个下降阶乘:

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

I just wrote my own:

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

Also, you can define a falling factorial:

def fall_fact(n,k)
  if k <= 0
    1
  else
    n*fall_fact(n - 1, k - 1)
  end
end
饭团 2024-08-31 18:12:25

怀着对所有参与并花时间帮助我们的人的崇高敬意,我想分享我对此处列出的解决方案的基准。
参数:

迭代次数 = 1000

n = 6

                                     user     system      total        real
Math.gamma(n+1)                   0.000383   0.000106   0.000489 (  0.000487)
(1..n).inject(:*) || 1            0.003986   0.000000   0.003986 (  0.003987)
(1..n).reduce(1, :*)              0.003926   0.000000   0.003926 (  0.004023)
1.upto(n) {|x| factorial *= x }   0.003748   0.011734   0.015482 (  0.022795)

对于 n = 10

  user     system      total        real
0.000378   0.000102   0.000480 (  0.000477)
0.004469   0.000007   0.004476 (  0.004491)
0.004532   0.000024   0.004556 (  0.005119)
0.027720   0.011211   0.038931 (  0.058309)

With high respect to all who participated and spent their time to help us, I would like to share my benchmarks of the solutions listed here.
Params:

iterations = 1000

n = 6

                                     user     system      total        real
Math.gamma(n+1)                   0.000383   0.000106   0.000489 (  0.000487)
(1..n).inject(:*) || 1            0.003986   0.000000   0.003986 (  0.003987)
(1..n).reduce(1, :*)              0.003926   0.000000   0.003926 (  0.004023)
1.upto(n) {|x| factorial *= x }   0.003748   0.011734   0.015482 (  0.022795)

For n = 10

  user     system      total        real
0.000378   0.000102   0.000480 (  0.000477)
0.004469   0.000007   0.004476 (  0.004491)
0.004532   0.000024   0.004556 (  0.005119)
0.027720   0.011211   0.038931 (  0.058309)
雨落□心尘 2024-08-31 18:12:25

只需调用此函数

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

示例即可

factorial(3)
factorial(11)

Just call this function

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

examples

factorial(3)
factorial(11)
战皆罪 2024-08-31 18:12:25

使用 Math.gamma.floor 是一种简单的方法来生成近似值,然后将其向下舍入为正确的整数结果。应该适用于所有整数,如有必要,请包括输入检查。

Using Math.gamma.floor is an easy way to produce an approximation and then round it back down to the correct integer result. Should work for all Integers, include an input check if necessary.

囍笑 2024-08-31 18:12:25

这只是另一种方法,尽管实际上没有必要。

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

Just another way to do it, although it really isn't necessary.

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
泛滥成性 2024-08-31 18:12:25

您可能会发现 Ruby 功能请求很有用。它包含一个重要的 补丁,其中包含 演示 Bash 脚本。简单循环与批次中提供的解决方案之间的速度差异实际上可以是 100 倍(数百倍)。全部用纯 Ruby 编写。

You will probably find a Ruby feature request useful. It contains a nontrivial patch that includes a demo Bash script. The speed difference between a naive loop and the solution presented in the batch can be literally 100x (hundred fold). Written all in pure Ruby.

黄昏下泛黄的笔记 2024-08-31 18:12:25

这是我的版本,尽管它不那么干净,但对我来说似乎很清楚。

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

这是我的 IRB 测试线,显示了每个步骤。

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

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
眼藏柔 2024-08-31 18:12:25

当有一个用于此确切目的的内置迭代器时,为什么标准库需要阶乘方法?它被称为upto

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

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

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

factorial = 1
1.upto(10) {|x| factorial *= x }
factorial
 => 3628800

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
热鲨 2024-08-31 18:12:25
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
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
铁憨憨 2024-08-31 18:12:25

还有另一种方式(=

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)

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)
残花月 2024-08-31 18:12:25

还有一种方法可以做到这一点:

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

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

fact(6) => 720

Just one more way to do it:

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

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

fact(6) => 720
紫竹語嫣☆ 2024-08-31 18:12:25

在 Ruby 中,阶乘的标准库函数不可用。我们可以用这种方式在 ruby​​ 中创建一个简单的阶乘函数。

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

puts factorial_number(6) #Output is 720   => (6*5*4*3*2*1)
puts factorial_number(8) #Output is 40320 => (8*7*6*5*4*3*2*1)

In Ruby standard library function for factorial is not available. We can make a simple function of factorial in ruby in this way.

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

puts factorial_number(6) #Output is 720   => (6*5*4*3*2*1)
puts factorial_number(8) #Output is 40320 => (8*7*6*5*4*3*2*1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文