You are Here: Home >< Maths

# Python recursion code help watch

1. So i need to be able to write using recursion

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
2. 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

Code:
```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.
3. will'o'wisp2 I've just edited my post in case you saw it while there were mistakes in it.
4. (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

Code:
```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 ._.
5. (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?
6. (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/attachmen...67/unknown.png
7. (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/attachmen...67/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.
8. (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/attachmen...74/unknown.png
9. (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/attachmen...74/unknown.png
here
10. (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/attachmen...74/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.

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

return 3 + timesby3(b-1)

print timesby3(12)```
11. (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.

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

return 3 + timesby3(b-1)

print timesby3(12)```
https://cdn.discordapp.com/attachmen...79/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
12. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...79/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.
13. (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
14. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...79/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).
15. (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.
16. (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/attachmen...11/unknown.png
17. (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.
18. (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 -=
19. (Original post by will'o'wisp2)
i don't understand what += means? same for -=
x = x+3

is the same as

x += 3
20. (Original post by will'o'wisp2)
nope i can't get it to work either
https://cdn.discordapp.com/attachmen...11/unknown.png
This is looking better but I think your second return statement shouldn't be indented. There's another mistake but try this first.

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

How do you think you'll do?

### University open days

Wed, 25 Jul '18
2. University of Buckingham
Wed, 25 Jul '18
3. Bournemouth University
Wed, 1 Aug '18
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