You are Here: Home >< Maths

# Python recursion code help watch

1. (Original post by Notnek)
Do you mean that you want it to multply the previous number by 3?

I'm not sure why you want it to do this. I recommend making a function that outputs 3 x n if you input n. Then you can extend this to a multiply(a,b) function.
lemme just read what i was supposed to do again...]
ok so i'm supposed to write code using recursion for multiplication using addition

right so i need not just
def multiply(a,b)
if a==1
return 0
if b==1
return 0
return a+a*(b-1)

but i think i need another variable in there because this is suppsoed to be for any 2 numbers a and b
2. (Original post by Blue_Cow)
Once you've done this, I suggest trying to learn Haskell. That will force you to do/learn recursion properly.
I was going to look at Haskell/Erlang at one point but never got round to it. I used to be a fan of making recursive algorithms in Prolog but I don't think that's taught much in unis
3. (Original post by will'o'wisp2)
lemme just read what i was supposed to do again...]
ok so i'm supposed to write code using recursion for multiplication using addition

right so i need not just
def multiply(a,b)
if a==1
return 0
if b==1
return 0
return a+a*(b-1)

but i think i need another variable in there because this is suppsoed to be for any 2 numbers a and b
This is not using recursion. You need a function that calls itself.

As I've already said, you should try fixing your multiply by 3 function before tackling a multiply any two numbers function.
4. (Original post by Notnek)
I was going to look at Haskell/Erlang at one point but never got round to it. I used to be a fan of making recursive algorithms in Prolog but I don't think that's taught much in unis
PRSOM

I've never done Prolog before and looking it doesn't look like it's taught here at undergrad level.
5. (Original post by Blue_Cow)
PRSOM

I've never done Prolog before and looking it doesn't look like it's taught here at undergrad level.
No I didn't think it would be. Sometimes it's taught alongside a course in formal logic.
6. (Original post by Notnek)
No I didn't think it would be. Sometimes it's taught alongside a course in formal logic.
Makes sense given the paradigm it's classed as
7. (Original post by Notnek)
This is not using recursion. You need a function that calls itself.

As I've already said, you should try fixing your multiply by 3 function before tackling a multiply any two numbers function.
https://cdn.discordapp.com/attachmen...31/unknown.png
i can't seem to get 9 out whatever i do
8. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...31/unknown.png
i can't seem to get 9 out whatever i do
Your problem is your base case (the if statement). You are saying that multiply(1) = 0 but actually multiply(1) = 3. Does this make sense?

You could also use multiply(0) = 0 as a smaller base case and then your function will work for n = 0.

You really need to be able to correct a mistake like this yourself by thinking about what your function does.
9. (Original post by Notnek)
Your problem is your base case (the if statement). You are saying that multiply(1) = 0 but actually multiply(1) = 3. Does this make sense?

You could also use multiply(0) = 0 as a smaller base case and then your function will work for n = 0.

You really need to be able to correct a mistake like this yourself by thinking about what your function does.
fiddled around and got 10 thanks so then now to put this in more general terms
10. (Original post by Notnek)
Your problem is your base case (the if statement). You are saying that multiply(1) = 0 but actually multiply(1) = 3. Does this make sense?

You could also use multiply(0) = 0 as a smaller base case and then your function will work for n = 0.

You really need to be able to correct a mistake like this yourself by thinking about what your function does.
https://cdn.discordapp.com/attachmen...07/unknown.png
is this it?
11. That will multiply a number by 4.

If you want a more general function to multiply two numbers then you'll need another parameter in your function i.e. multiply(a,b).
12. (Original post by Notnek)
That will multiply a number by 4.

If you want a more general function to multiply two numbers then you'll need another parameter in your function i.e. multiply(a,b).
right ok, i'll see what i can do then,....
13. (Original post by Notnek)
That will multiply a number by 4.

If you want a more general function to multiply two numbers then you'll need another parameter in your function i.e. multiply(a,b).
https://cdn.discordapp.com/attachmen...56/unknown.png

i don't understand what's happening i don't know what tuple is
14. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...56/unknown.png

i don't understand what's happening i don't know what tuple is
A tuple is pretty much a list that you can't amend.

Try moving the -1 out of the (b,c) bit outside, so it looks like this on line 8

return a+(multiply(b,c)) - 1
15. (Original post by Blue_Cow)
A tuple is pretty much a list that you can't amend.

Try moving the -1 out of the (b,c) bit outside, so it looks like this on line 8

return a+(multiply(b,c)) - 1
https://cdn.discordapp.com/attachmen...16/unknown.png

it's done this which means that it doesn't work but it's not telling what has gone wrong.
16. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...16/unknown.png

it's done this which means that it doesn't work but it's not telling what has gone wrong.
It's doing recursion non-stop. There is something illogical in your code.
17. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...16/unknown.png

it's done this which means that it doesn't work but it's not telling what has gone wrong.
Firstly you don't need the constant a = 2. I'm not sure what that is meant to be doing. I think you should be thinking about this more before hacking away at it.

multiply(b,0) = 0

(because b x 0 = 0)

Then since multiplication is repeated addition you have

multiply(b,1) = b + multiply(b,0)
multiply(b,2) = b + multiply(b,1)
multiply(b,3) = b + multiply(b,2)

So multiply(b,c) = b + multiply(b,c-1) for c > 0.

The two bold lines need to be implemented in your code.
18. (Original post by Notnek)
Firstly you don't need the constant a = 2. I'm not sure what that is meant to be doing. I think you should be thinking about this more before hacking away at it.

multiply(b,0) = 0

(because b x 0 = 0)

Then since multiplication is repeated addition you have

multiply(b,1) = b + multiply(b,0)
multiply(b,2) = b + multiply(b,1)
multiply(b,3) = b + multiply(b,2)

So multiply(b,c) = b + multiply(b,c-1) for c > 0.

The two bold lines need to be implemented in your code.
giving up after spending all this time looking at my notes and your guidance i searched google for help which got me
def multiply(a,b):
if a==1
....return b
if a==0:
....return 0
return b+multiply(a-1,b)

print(multiply(2,4))

### Related university courses

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: November 5, 2017
Today on TSR

### Top unis in Clearing

Tons of places at all these high-ranking unis

Poll
Useful resources

### Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

### How to use LaTex

Writing equations the easy way

### Study habits of A* students

Top tips from students who have already aced their exams