Python Forum

Full Version: Print every multiplication of recursion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to print every multiplication step that happens in the following recursion code. I tried printing the return statement itself before returning it (as you can see) but that doesn't help. How should I go about it?

def expo(a,n):
    if n == 0:
        return 1
    elif n%2 == 0:
        print(expo(a*a, n/2))
        return expo(a*a, n/2)
    elif n%2 == 1:
        print(a*expo(a*a, (n-1)/2))
        return a*expo(a*a, (n-1)/2)
The solution to the problem is:

def expo(a,n):
    if n == 0:
        return 1
    elif n%2 == 0:
        return expo(a*a, n/2)
    elif n%2 == 1:
        return a*expo(a*a, (n-1)/2)

a = int(input("Enter a: "))
n = int(input("Enter n: "))

print(expo(a,n))
A test output is:
Enter a: 4
Enter n: 4
256

def expo(a,n):
    if n == 0:
        return 1
    elif n%2 == 0:
        return expo(a*a, n/2)
    elif n%2 == 1:
        return a*expo(a*a, (n-1)/2)

a = int(input("Enter a: "))
n = int(input("Enter n: "))

print(expo(a,n))
(Apr-23-2018, 08:31 PM)pyghorz Wrote: [ -> ]The solution to the problem is:
 def expo(a,n): if n == 0: return 1 elif n%2 == 0: return expo(a*a, n/2) elif n%2 == 1: return a*expo(a*a, (n-1)/2) a = int(input("Enter a: ")) n = int(input("Enter n: ")) print(expo(a,n)) 
A test output is:
Enter a: 4 Enter n: 4 256 

def expo(a,n): if n == 0: return 1 elif n%2 == 0: return expo(a*a, n/2) elif n%2 == 1: return a*expo(a*a, (n-1)/2) a = int(input("Enter a: ")) n = int(input("Enter n: ")) print(expo(a,n)) 

I already have the solution. What I need to do is modify the function so that whenever it performs a multiplication, it prints out the multiplication process e.g. if it does 2*3, it prints out "2*3 = 6". I hope that is clear. Thanks.
You can replace a * b with a function call mul(a, b) and do the print in mul():
def mul(a, b):
    res = a * b
    print('{} * {} ⁼ {}'.format(a, b, res))
    return res

def expo(a,n):
    if n == 0:
        return 1
    elif n%2 == 0:
        return expo(mul(a, a), n/2)
    elif n%2 == 1:
        return mul(a, expo(mul(a, a), (n-1)/2))
    
if __name__ == '__main__':
    expo(3, 25)
Output:
3 * 3 ⁼ 9 9 * 9 ⁼ 81 81 * 81 ⁼ 6561 6561 * 6561 ⁼ 43046721 43046721 * 43046721 ⁼ 1853020188851841 43046721 * 1 ⁼ 43046721 6561 * 43046721 ⁼ 282429536481 3 * 282429536481 ⁼ 847288609443
(Apr-24-2018, 06:37 AM)Gribouillis Wrote: [ -> ]You can replace a * b with a function call mul(a, b) and do the print in mul():
 def mul(a, b): res = a * b print('{} * {} ⁼ {}'.format(a, b, res)) return res def expo(a,n): if n == 0: return 1 elif n%2 == 0: return expo(mul(a, a), n/2) elif n%2 == 1: return mul(a, expo(mul(a, a), (n-1)/2)) if __name__ == '__main__': expo(3, 25) 
Output:
3 * 3 ⁼ 9 9 * 9 ⁼ 81 81 * 81 ⁼ 6561 6561 * 6561 ⁼ 43046721 43046721 * 43046721 ⁼ 1853020188851841 43046721 * 1 ⁼ 43046721 6561 * 43046721 ⁼ 282429536481 3 * 282429536481 ⁼ 847288609443

Somebody showed me a different way to do but this one seems to do it as well. Thank you!