递归Python函数因子

时间:2015-02-15 11:17:48

标签: python function recursion factorial

  

编写一个名为odd_factorial(x)的递归Python函数,如果x为奇数,则输入x >= 53 × 5 × 7 × . . . × x)计算x;如果3 × 5 × . . . × (x − 1)是偶数,则为x

我有递归,但我不太确定如何确保第一个循环x >= 5

def odd_factorial(x):
    if x%2 == 1:
        if x == 3:
            return 3
        else:
            return x*odd_factorial(x-2)
    else:
        x -= 1
        if x == 3:
            return 3
        else:
            return x*odd_factorial(x-2)

6 个答案:

答案 0 :(得分:1)

这将更快更干净,因为它不会在实际递归中进行调节。它仍然是一个函数,尽管另一个嵌套在。

def odd_factorial(x):
    def do_fact(x):
        if x == 3:
            return 3

        return x * do_fact(x - 2)

    if x < 5:
        raise ValueError("X must be at least 5")

    if not x % 2:
        x -= 1

    return do_fact(x)

运行:

>>> odd_factorial(5)
15
>>> odd_factorial(6)
15
>>> odd_factorial(7)
105
>>> odd_factorial(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 9, in odd_factorial
    raise ValueError("X must be at least 5")
ValueError: X must be at least 5
>>> odd_factorial(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 9, in odd_factorial
    raise ValueError("X must be at least 5")
ValueError: X must be at least 5

另一种方法是直接为15odd_factorial(5)返回odd_factorial(6)

def odd_factorial(x):
    if x < 5:
        raise ValueError("x must be at least 5")
    if x % 2 == 0:
        x -= 1     
    if x == 5:
        return 15
    return x * odd_factorial(x - 2)

答案 1 :(得分:0)

添加一个额外的检查设置一个标志,以便您捕获输入的值&lt; 5如果是第一次调用函数,你也可以从代码中删除else,因为你一次只能返回任何一行:

def odd_factorial(x, flag=True):
    if x < 5 and  flag:
        return "Only numbers >= 5"
    if x % 2: # or if x & 1
        return x * odd_factorial(x - 2, False) if x != 3 else 3
    x -= 1
    return x * odd_factorial(x - 2, False)

In [32]: odd_factorial(5)
Out[32]: 15

In [33]: odd_factorial(3)
Out[33]: 'Only numbers >= 5'

In [34]: odd_factorial(7)
Out[34]: 105

答案 2 :(得分:0)

使用inner功能,为您节省额外的参数:

>>> def odd_factorial(input):
     def odd_factorial_inner(x):
         if x%2 == 1:
             if x == 3:
                 return 3
             else:
                 return x*odd_factorial_inner(x-2)
         else:
             x -= 1
             if x == 3:
                 return 3
             else:
                 return x*odd_factorial_inner(x-2)
     if (input >= 5):
         return odd_factorial_inner(input)
     else:
         print "input is smaller than 5"

>>> odd_factorial(9)
945
>>> odd_factorial(4)
Input is smaller than 5.

答案 3 :(得分:0)

简单复杂:)

def odd_factorial(x, signal=None):
    if x < 5 and signal==None:
        return "number should be greater than 5"
    else:
        if x%2 == 1:
            if x == 3:
                return 3
            else:
                return x*odd_factorial(x-2, signal="r")
        else:
            x -= 1
            if x == 3:
                return 3
            else:
                return x*odd_factorial(x-2, signal="r")

答案 4 :(得分:0)

为什么不简单地改变它的计算方式:

def odd_factorial(x):
    if x%2 == 0:
        x -= 1
    if x == 5:
        return 3*5
    elif x < 5:
        raise ValueError("Input should not be < 5")
    else:
        return x*odd_factorial(x-2)

这很好,因为你不像以前那样重复自己(使用偶数/奇数检查),你仍然遵循规范!

答案 5 :(得分:0)

这是一个非常简单的递归因子程序,仅使用奇数

#Factorial using Recursion
n = int(input("Enter the n value for Factorial:"))
def factorial(n):
if n<=1:
    return 1
else:
    return n*factorial(n-2)

print(factorial(n))