如何计算Ruby中数字的阶乘?

时间:2022-11-11 16:50:03

Alright, so I asked an earlier question on my syntax error. I got rid of the errors, but the program doesn't do what it was intended to do. My math is wrong and doesn't find the number of trailing zeros. Here is my code:

好吧,所以我问了一个关于语法错误的早期问题。我摆脱了错误,但程序没有按照预期的方式完成。我的数学错误,并没有找到尾随零的数量。这是我的代码:

num = " "
a = 0
sumOfFact = 1

def factorial
    num = gets.to_i
    a = num
    (1..num).each do |a|
        if a != 1
            sumOfFact *= a
            a -= 1
        else
            break
        end
    end
end

for c in 1..sumOfFact
    if sumOfFact / c == 10
            zeros += 1
    end
end

factorial()
puts sumOfFact
puts zeros

4 个解决方案

#1


2  

Well, first, you should do the gets outside your method. Your method should accept a param. Second, why do you need the condition?

嗯,首先,你应该在你的方法之外进行。你的方法应该接受一个参数。第二,为什么你需要这个条件?

You want the multiplication from 1 to n to get the factorial. You should get started with this:

您希望从1到n的乘法得到阶乘。你应该开始这个:

def factorial(n)
  total = 1
  (1..n).each do |n|
    total *= n   
  end
  total
end

puts factorial(gets.to_i)

Next is factorial with inject in case you want to learn new syntax :-)

接下来是注入因子,以防您想学习新语法:-)

def factorial(n)
  n == 0? 1 : (1..n).inject(1) { |total, i| total*= i; total }
end

puts factorial(gets.to_i)

As @pjs commented below, here's a beautiful way of doing factorial!

正如@pjs在下面评论的那样,这是一个做阶乘的美妙方式!

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

And, a final enhancement:

并且,最后的增强:

def factorial(n)
  (1..n).inject(1, :*)
end

#2


1  

Supposing that n is a non-negative integer number, you can define a method to calculate the factorial:

假设n是非负整数,您可以定义一个计算阶乘的方法:

def factorial(n)
  tot = 1
  (1..n).each do |n|
    tot *= x
  end

  tot
end

Examples of its usage:

其用法示例:

puts factorial(0) # 1 
puts factorial(1) # 1
puts factorial(2) # 2 
puts factorial(3) # 6
puts factorial(4) # 24
puts factorial(5) # 120 

If you wan't to read the user input, call it like this:

如果您不想阅读用户输入,请按以下方式调用:

puts 'Type the non-negative integer:'
n = gets.to_i
puts factorial(n)

#3


0  

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

Or you could just simply write:

或者你可以简单地写:

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

#4


0  

Try this too. Hope this helps anyone having the same problem in some way.

试试这个。希望这可以帮助任何人以某种方式遇到同样的问题。

Method for finding the factorial of any number:

查找任意数量的阶乘的方法:

def factorial(number)
    for i in 1...number do
        number *= i
    end
    number
end

puts factorial(5)

#1


2  

Well, first, you should do the gets outside your method. Your method should accept a param. Second, why do you need the condition?

嗯,首先,你应该在你的方法之外进行。你的方法应该接受一个参数。第二,为什么你需要这个条件?

You want the multiplication from 1 to n to get the factorial. You should get started with this:

您希望从1到n的乘法得到阶乘。你应该开始这个:

def factorial(n)
  total = 1
  (1..n).each do |n|
    total *= n   
  end
  total
end

puts factorial(gets.to_i)

Next is factorial with inject in case you want to learn new syntax :-)

接下来是注入因子,以防您想学习新语法:-)

def factorial(n)
  n == 0? 1 : (1..n).inject(1) { |total, i| total*= i; total }
end

puts factorial(gets.to_i)

As @pjs commented below, here's a beautiful way of doing factorial!

正如@pjs在下面评论的那样,这是一个做阶乘的美妙方式!

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

And, a final enhancement:

并且,最后的增强:

def factorial(n)
  (1..n).inject(1, :*)
end

#2


1  

Supposing that n is a non-negative integer number, you can define a method to calculate the factorial:

假设n是非负整数,您可以定义一个计算阶乘的方法:

def factorial(n)
  tot = 1
  (1..n).each do |n|
    tot *= x
  end

  tot
end

Examples of its usage:

其用法示例:

puts factorial(0) # 1 
puts factorial(1) # 1
puts factorial(2) # 2 
puts factorial(3) # 6
puts factorial(4) # 24
puts factorial(5) # 120 

If you wan't to read the user input, call it like this:

如果您不想阅读用户输入,请按以下方式调用:

puts 'Type the non-negative integer:'
n = gets.to_i
puts factorial(n)

#3


0  

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

Or you could just simply write:

或者你可以简单地写:

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

#4


0  

Try this too. Hope this helps anyone having the same problem in some way.

试试这个。希望这可以帮助任何人以某种方式遇到同样的问题。

Method for finding the factorial of any number:

查找任意数量的阶乘的方法:

def factorial(number)
    for i in 1...number do
        number *= i
    end
    number
end

puts factorial(5)