Python Forum
Print every multiplication of recursion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print every multiplication of recursion
#1
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)
Reply
#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))
Reply
#3
(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.
Reply
#4
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
Reply
#5
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiplication Table code alexsendlegames100 3 1,359 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  Try to solve GTG multiplication table problem. Frankduc 6 1,986 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Need help with my Python code (Multiplication) NeedHelpPython 2 1,676 Oct-04-2021, 12:09 PM
Last Post: Pedroski55
  List conversion and multiplication johnkyp 5 3,159 Jan-02-2020, 08:20 AM
Last Post: perfringo
  Matrix Multiplication Issue VIJENDRA 1 1,858 Dec-19-2019, 06:16 PM
Last Post: Gribouillis
  Multiplication between a list and a variable doug2019 2 2,154 Oct-08-2019, 04:10 AM
Last Post: doug2019
  Multiplication Table number margins CJ707 4 2,426 Sep-18-2019, 02:16 PM
Last Post: CJ707
  multiplication by successive addition Zebrol 1 3,507 Sep-14-2019, 05:37 PM
Last Post: ichabod801
  float multiplication - unexpected output inesk 3 3,320 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE
  Nothing happens - its a simple multiplication table! tjnichols 10 5,925 Apr-03-2018, 05:13 PM
Last Post: tjnichols

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020