0

Recursive Multiplication Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 7 × 4 = 4 + 4 + 4 + 4 + 4 + 4 + 4

Yes, this is my home work from like three weeks ago, but i just cant figure this thing out.

def mult(x, y):

     x = int(input("enter x"))
     y = int(input("enter y"))
     i = 0
     for i in range(y):
             x = x + (x - 1)
             i = i + 1
             print(x)
 mult(x, y)

This is what i have, but if i put 10 for x and 10 for y i get 1024 instead of 100. what is going on here?

2
  • 1
    Your method isn't recursive Commented Apr 23, 2016 at 17:42
  • Not sure how you get 1024; your posted code would produce 9217 for x = 10 and y = 10.. It'll print out another 9 numbers before that point, but none of those is ever equal to 1024 either. Commented Apr 23, 2016 at 18:02

3 Answers 3

2

You are incrementing x each time you add x - 1 to it. So for 10 times 10 you start with 10, then add 10 - 1 = 9, making 19, and store that back in x. The next iteration you add 19 - 1 = 18 to x = 19, so now you have x = 37. Then you add 37 - 1 = 36 to 37, so now you have x = 73. Next you'd add 72, and you are already way past the real answer of 100, in just 4 steps; there are another 6 to go!

Keep your sum in a separate variable, and start that at zero. Add y to that each time:

total = 0
for i in range(x):
    total = total + y

print(total)

There is no need to subtract 1 from x, the for loop takes care of counting for you.

None of this is actually using recursion. This is a recursive version:

def mult(x, y):
    if x == 0:
        return 0
    return y + mult(x - 1, y)

So if x is 0, there is no multiplication and you return 0, otherwise you sum y to the multiplication of y and x - 1. Yes, here you do subtract one.

Sign up to request clarification or add additional context in comments.

9 Comments

x = 0 y = 0 def mult(x, y): x = int(input("enter x")) y = int(input("enter y")) total = mult(x, 1) if x == 0: return 0 return y + mult(x - 1, y) print(total) mult(x, y)
@RichardBermudezKenjaTrading move the input() calls out of the function. You don't want to ask for new values each recursive call. So instead of x = 0, use x = int(input('Enter x: '), same for y, then call the mult(x, y) function.
x = int(input("enter x")) y = int(input("enter y")) def mult(x, y): total = mult(x, 1) if x == 0: return 0 return y + mult(x - 1, y) print(mult(x,y)) mult(x, y)
it goes in to an infinite loop and then says its reached its maximum recursion
@RichardBermudezKenjaTrading your version does not match mine, does it. You call mult() on the first line of the function, passing in x and 1. That'll only lead to the function forever calling itself. Why did you add that call?
|
1

The recursion is:

  mult(x, y) = x + mult(x, y-1)
  mult(x, 1) = x  // This is the base condition

Now, you should be able to implement it

Comments

0

Here is the working code, I normally use C++ print "welcome to recursive function" x = input("Enter the x: " ) y = input("Enter the y: " )

CONSTANT = x def recursive(x, y): if y == 0: x = 0 return x if y == 1: return x if y > 1: x = x + recursive(x, y-1) return x x = recursive(x, y) print "The resulting value is ", x

  1. List item

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.