The Student Room Group

Python recursion code help

So i need to be able to write using recursion

multiplication using addition
powers using multiplication
factorials and
the fibonacci sequence

so far i know that you layout roughly in this form of

def fib(n)
a=............
b=............
return ...... <- is the nth term

print(fib(2))

def main()

then you got your function whatnot but i need help on the above topics i'm not realyl sure where to start

Scroll to see replies

Reply 1
@will'o'wisp2


So I don't know python but I can help you with the general form of a simple recursive algorithm:

Let's say multby3(n) outputs 3 x n.

So multby3(0) = 0 <- this is known as the base case.

multby3(1) = 3 = multby3(0) + 3
multby3(2) = 6 = multby3(1) + 3
multby3(3) = 9 = multby3(2) + 3
...

So what you have is

multby3(0) = 0
multby3(n) = multby3(n-1) + 3 , (n > 0)

So as pseudo-code this would be


multby3(n) {

// base case
if n = 0 {
output 0 and exit
}

// n > 0
output multby3(n-1) + 3

}



If your first function needs to be able to multiply any two numbers then you'll have to think about how to make a mult(a,b) function but you can follow a similar method.

If you don't understand any of this please let me know.
(edited 6 years ago)
Reply 2
@will'o'wisp2 I've just edited my post in case you saw it while there were mistakes in it.
(edited 6 years ago)
Original post by Notnek
@will'o'wisp2


So I don't know python but I can help you with the general form of a simple recursive algorithm:

Let's say multby3(n) outputs 3 x n.

So multby3(0) = 0 <- this is known as the base case.

multby3(1) = 3 = multby3(0) + 3
multby3(2) = 6 = multby3(1) + 3
multby3(3) = 9 = multby3(2) + 3
...

So what you have is

multby3(0) = 0
multby3(n) = multby3(n-1) + 3 , (n > 0)

So as pseudo-code this would be




multby3(n) {

// base case
if n = 0 {
output 0 and exit
}

// n > 0
output multby3(n-1) + 3

}





If your first function needs to be able to multiply any two numbers then you'll have to think about how to make a mult(a,b) function but you can follow a similar method.

If you don't understand any of this please let me know.


Original post by Notnek
@will'o'wisp2 I've just edited my post in case you saw it while there were mistakes in it.


i understand what to do now but when i put the code into python, it doesnt do what i want it to do ._.
Reply 4
Original post by will'o'wisp2
i understand what to do now but when i put the code into python, it doesnt do what i want it to do ._.

Can you post your code here and we can take a look?
Original post by Notnek
Can you post your code here and we can take a look?


someone did this for me but i don't really get most of it, isn't there another way to do 5x4 for example?

like i need to do axb basically where a and b can be any number i choose but i'm not really sure how to change that so it works like that

https://cdn.discordapp.com/attachments/322819040059850757/376735193358270467/unknown.png
Original post by will'o'wisp2
someone did this for me but i don't really get most of it, isn't there another way to do 5x4 for example?

like i need to do axb basically where a and b can be any number i choose but i'm not really sure how to change that so it works like that

https://cdn.discordapp.com/attachments/322819040059850757/376735193358270467/unknown.png


Which lines do you specifically not understand?

A good way to try and see what the program is doing, is to run it on pen and paper to see what it does to a certain input.
Original post by Blue_Cow
Which lines do you specifically not understand?

A good way to try and see what the program is doing, is to run it on pen and paper to see what it does to a certain input.


lines 1,3,4,5,7,8,10,11
but anyway
i made some new code but i can't get this one right, i just need to do axb where a and b are any numbers of my choosing, so i've decided to do 3x9 where a is 9 and b is 3

so far i got this

https://cdn.discordapp.com/attachments/322819040059850757/376739815460175874/unknown.png
Original post by Notnek
Can you post your code here and we can take a look?


Original post by will'o'wisp2
lines 1,3,4,5,7,8,10,11
but anyway
i made some new code but i can't get this one right, i just need to do axb where a and b are any numbers of my choosing, so i've decided to do 3x9 where a is 9 and b is 3

so far i got this

https://cdn.discordapp.com/attachments/322819040059850757/376739815460175874/unknown.png


here
Reply 9
Original post by will'o'wisp2
lines 1,3,4,5,7,8,10,11
but anyway
i made some new code but i can't get this one right, i just need to do axb where a and b are any numbers of my choosing, so i've decided to do 3x9 where a is 9 and b is 3

so far i got this

https://cdn.discordapp.com/attachments/322819040059850757/376739815460175874/unknown.png

Your code doesn't make much sense to me (although I don't know Python). Why do you need two separate functions? And muliple(a) returns an expression containing n but n is not defined in the multiple function? I'm not sure what you're trying to do here.

You could try using this example instead : here is some simple Python code that I just wrote which multiplies a positive integer by 3. Try to understand what it does and then adapt it.






def timesby3(b):
if b == 0:
return 0

return 3 + timesby3(b-1)

print timesby3(12)





(edited 6 years ago)
Original post by Notnek
Your code doesn't make much sense to me (although I don't know Python). Why do you need two separate functions? And muliple(a) returns an expression containing n but n is not defined in the multiple function? I'm not sure what you're trying to do here.

You could try using this example instead : here is some simple Python code that I just wrote which multiplies a positive integer by 3. Try to understand what it does and then adapt it.







def timesby3(b):
if b == 0:
return 0

return 3 + timesby3(b-1)

print timesby3(12)









https://cdn.discordapp.com/attachments/322819040059850757/376743716263559179/unknown.png

cus then it does this

i justneed to put this in general terms so instead of 3 in need to say a but i dont' know how to do it tho
Reply 11
Original post by will'o'wisp2
https://cdn.discordapp.com/attachments/322819040059850757/376743716263559179/unknown.png

cus then it does this

i justneed to put this in general terms so instead of 3 in need to say a but i dont' know how to do it tho

You're saying "if a = 1" but what is a? The only parameter of the multiply function is n.

@Blue_Cow do you know Python? If you do then it may be better if you help rather than me.
Original post by Notnek
You're saying "if a = 1" but what is a? The only parameter of the multiply function is n.

@Blue_Cow do you know Python? If you do then it may be better if you help rather than me.


a number which i choose later on at the bottom when i want to print something out
Reply 13
Original post by will'o'wisp2
https://cdn.discordapp.com/attachments/322819040059850757/376743716263559179/unknown.png

cus then it does this

i justneed to put this in general terms so instead of 3 in need to say a but i dont' know how to do it tho

Do you have a function that works with 3? If not you need to start with that. Or are you adapting my code (I don't think you are).
Reply 14
Original post by FriendlyPenguin
x

The OP is trying to write functions using recursion so I don't think this code is what they are looking for.

Plus as always it is better to guide the OP instead of giving a solution.
(edited 6 years ago)
Original post by Notnek
Do you have a function that works with 3? If not you need to start with that. Or are you adapting my code (I don't think you are).


nope i can't get it to work either
https://cdn.discordapp.com/attachments/322819040059850757/376746991256076311/unknown.png
Original post by FriendlyPenguin
Why is he using Classes rather than just functions?

Why not use something like this:

def multiply(a, b):
>>>>x = 0
>>>>while b>0: # Assuming b is an integer
>>>>>>>>x += a
>>>>>>>>b -= 1
>>>>return x
There's a "don't post full solutions" rule for this forum.
Original post by FriendlyPenguin
Why is he using Classes rather than just functions?

Why not use something like this:

def multiply(a, b):
>>>>x = 0
>>>>while b>0: # Assuming b is an integer
>>>>>>>>x += a
>>>>>>>>b -= 1
>>>>return x


i don't understand what += means? same for -=
Original post by will'o'wisp2
i don't understand what += means? same for -=


x = x+3

is the same as

x += 3
Reply 19

This is looking better but I think your second return statement shouldn't be indented. There's another mistake but try this first.

Quick Reply

Latest