制作递归ruby函数

时间:2014-10-25 22:16:43

标签: ruby function recursion

制作一个RECURSIVE ruby​​函数“double_fact(n)”,定义如下 -                  ñ!如果n = -1或n = 0或n = 1,则= 1;                           n(n - 2)!!除此以外。      输出double_fact()的结果,使其符合指定的值
     从命令行。

      //Hint: Ruby has the usual "and", "or" and "not" operators. You may  
        need "or" to test multiple conditions here. Also, doublefact(8) = 384.

1 个答案:

答案 0 :(得分:1)

问题陈述非常误导。您根本不需要任何布尔运算符,您只需将数学定义1:1转换为Ruby:

def doublefact(n)
  return 1 if (-1..1).include?(n)
  n * doublefact(n-2)
end