Search code examples
pythonpython-3.xmathrecursionradix

Python recursion function


I am trying to take a number, do floor division on it, until it becomes 0. This must be done using recursion with a base case. For example:

>>>Base(5,2)
2 ##(5//2)
1 ##(2//2)
0 ##(1//2)

This is what I have so far:

def Base(number,base):
    result=1
    if result==0:
        return False
    else:
        result=number//base
        return Base(result,base)

Solution

  • I think this is what you are looking for

    def Base(number,base):
    
            if number==0:
                return False
            else:
                number = number//base
                print(number)
                return Base(number,base)
    

    First of all using result = 1 should be removed as during every recursive call result will be reinitialized to 1 and the following if statement will never work. What you needed to do was to keep on dividing the number recursively till it reaches 0 and print False

    I hope it clears your doubt