这是我的递归方法的问题吗?

时间:2016-06-10 23:37:09

标签: python recursion big-o

在回应this question时,我试图提出自己的解决方案。

我在python 3.X中的解决方案

def efficientalgo(number, x):
    print("Start with this number... " + str(number))

    # See if a number is dividable by three, if so then divide by 3
    if number % 3 == 0:
        print("Dividing three...")
        number /= 3
        print(number)
        # use recursion to see if the number can be divided again
        number += efficientalgo(number, x)
        print("Returning number from division by three now...")
        return number


   # Divide a number by 2 if it is evenly dividable by 2
   if number % 2 == 0:
        print("Dividing two...")
        number /= 2
        print(number)
        # Use recursion to see if the number can be divided again
        number += efficientalgo(number, x)
        print("Returning number from division by two now...")
        print(number)
        return number


    # If a number is not one, then subtract one and call the
    if number != 1:
        print("Subtracting one now...")
        number -= 1
        print(number)
        # Use recursion to see if the number can be divided again
        number += efficientalgo(number, x)
        print(number)
        return number

    # If the number is one, return it and finish.
    if number == 1:
        print("Returning one now... " + str(number))
        return number

print(efficientalgo(100, 1))

这是一个“工作”pythonfiddle

输出

Start with this number... 100
Dividing two...
50.0
Start with this number... 50.0
Dividing two...
25.0
Start with this number... 25.0
Subtracting one now...
24.0
Start with this number... 24.0
Dividing three...
8.0
Start with this number... 8.0
Dividing two...
4.0
Start with this number... 4.0
Dividing two...
2.0
Start with this number... 2.0
Dividing two...
1.0
Start with this number... 1.0
Returning one now... 1.0
-----Above this line is the correct output I want-----

---------Below this line, I have no idea what is going on--------
Returning number from division by two now...
2.0
Returning number from division by two now...
4.0
Returning number from division by two now...
8.0
Returning number from division by three now...
40.0
Returning number from division by two now...
65.0
Returning number from division by two now...
115.0
115.0

正如你所看到的,我已经错误地实现了我的递归并且正在获得某种反馈循环,在程序中得到正确的答案,然后在我返回我的最终数字之后继续进行,在这种情况下是一个。

我不明白我在输出中标出的行下面发生了什么

1 个答案:

答案 0 :(得分:1)

号码应设置为= efficientalgo(..)而不是+=

def efficientalgo(number):
    print("Start with this number... " + str(number))
    # See if a number is dividable by three, if so then divide by 3
    if number % 3 == 0:
        print("Dividing three...")
        number /= 3
        print(number)
        # use recursion to see if the number can be divided again
        number =  efficientalgo(number)
    # Divide a number by 2 if it is evenly dividable by 2
    if number % 2 == 0:
        print("Dividing two...")
        number /= 2
        print(number)
        # Use recursion to see if the number can be divided again
        number = efficientalgo(number)

    # If a number is not one, then subtract one and call the
    if number != 1:
        print("Subtracting one now...")
        number -= 1
        print(number)
        # Use recursion to see if the number can be divided again
        number = efficientalgo(number)
   return number

一旦你这样做,就会得到预期的输出:

In [4]: efficientalgo(100)
Start with this number... 100
Dividing two...
50.0
Start with this number... 50.0
Dividing two...
25.0
Start with this number... 25.0
Subtracting one now...
24.0
Start with this number... 24.0
Dividing three...
8.0
Start with this number... 8.0
Dividing two...
4.0
Start with this number... 4.0
Dividing two...
2.0
Start with this number... 2.0
Dividing two...
1.0
Start with this number... 1.0
Out[4]: 1.0

或者简单地回复:

def efficientalgo(number):
    print("Start with this number... " + str(number))
    # See if a number is dividable by three, if so then divide by 3
    if number % 3 == 0:
        print("Dividing three...")
        number /= 3
        print(number)
        # use recursion to see if the number can be divided again
        return efficientalgo(number)

    # Divide a number by 2 if it is evenly dividable by 2
    if number % 2 == 0:
        print("Dividing two...")
        number /= 2
        print(number)
        # Use recursion to see if the number can be divided again
        return efficientalgo(number)

    # If a number is not one, then subtract one and call the
    if number != 1:
        print("Subtracting one now...")
        number -= 1
        print(number)
        # Use recursion to see if the number can be divided again
        return efficientalgo(number)

    return number