The Student Room Group

Python recursion code help

Scroll to see replies

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
Reply 41
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 :smile:
Reply 42
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.
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 :smile:


PRSOM :biggrin:

I've never done Prolog before and looking it doesn't look like it's taught here at undergrad level.
Reply 44
Original post by Blue_Cow
PRSOM :biggrin:

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.
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
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/attachments/322819040059850757/376767384469372931/unknown.png
i can't seem to get 9 out whatever i do
Reply 47

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.
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
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/attachments/322819040059850757/376777375192580107/unknown.png
is this it?
Reply 50

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).
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,....
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/attachments/322819040059850757/376783334237536256/unknown.png

i don't understand what's happening i don't know what tuple is
Original post by will'o'wisp2
https://cdn.discordapp.com/attachments/322819040059850757/376783334237536256/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
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/attachments/322819040059850757/376786700904431616/unknown.png

it's done this which means that it doesn't work but it's not telling what has gone wrong.
Original post by will'o'wisp2
https://cdn.discordapp.com/attachments/322819040059850757/376786700904431616/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.
Reply 56
Original post by will'o'wisp2
https://cdn.discordapp.com/attachments/322819040059850757/376786700904431616/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.

Your base case can be

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.
(edited 6 years ago)
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.

Your base case can be

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))

Quick Reply

Latest