Turn on thread page Beta
    • Thread Starter
    Offline

    15
    ReputationRep:
    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
    • Community Assistant
    • Study Helper
    Offline

    20
    ReputationRep:
    Community Assistant
    Study Helper
    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.
    • Community Assistant
    • Study Helper
    Offline

    20
    ReputationRep:
    Community Assistant
    Study Helper
    will'o'wisp2 I've just edited my post in case you saw it while there were mistakes in it.
    • Thread Starter
    Offline

    15
    ReputationRep:
    (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 ._.
    • Community Assistant
    • Study Helper
    Offline

    20
    ReputationRep:
    Community Assistant
    Study Helper
    (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?
    • Thread Starter
    Offline

    15
    ReputationRep:
    (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
    • Community Assistant
    Offline

    21
    ReputationRep:
    Community Assistant
    (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.
    • Thread Starter
    Offline

    15
    ReputationRep:
    (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
    • Thread Starter
    Offline

    15
    ReputationRep:
    (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
    • Community Assistant
    • Study Helper
    Offline

    20
    ReputationRep:
    Community Assistant
    Study Helper
    (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)
    • Thread Starter
    Offline

    15
    ReputationRep:
    (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
    • Community Assistant
    • Study Helper
    Offline

    20
    ReputationRep:
    Community Assistant
    Study Helper
    (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.
    • Thread Starter
    Offline

    15
    ReputationRep:
    (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
    • Community Assistant
    • Study Helper
    Offline

    20
    ReputationRep:
    Community Assistant
    Study Helper
    (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).
    • Community Assistant
    • Study Helper
    Offline

    20
    ReputationRep:
    Community Assistant
    Study Helper
    (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.
    • Thread Starter
    Offline

    15
    ReputationRep:
    (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
    Offline

    18
    ReputationRep:
    (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.
    • Thread Starter
    Offline

    15
    ReputationRep:
    (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 -=
    • Community Assistant
    Offline

    21
    ReputationRep:
    Community Assistant
    (Original post by will'o'wisp2)
    i don't understand what += means? same for -=
    x = x+3

    is the same as

    x += 3
    • Community Assistant
    • Study Helper
    Offline

    20
    ReputationRep:
    Community Assistant
    Study Helper
    (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.
 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: November 5, 2017

University open days

  1. University of Bradford
    University-wide Postgraduate
    Wed, 25 Jul '18
  2. University of Buckingham
    Psychology Taster Tutorial Undergraduate
    Wed, 25 Jul '18
  3. Bournemouth University
    Clearing Campus Visit Undergraduate
    Wed, 1 Aug '18
Poll
How are you feeling in the run-up to Results Day 2018?
Useful resources

Make your revision easier

Maths

Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

Equations

How to use LaTex

Writing equations the easy way

Student revising

Study habits of A* students

Top tips from students who have already aced their exams

Study Planner

Create your own Study Planner

Never miss a deadline again

Polling station sign

Thinking about a maths degree?

Chat with other maths applicants

Can you help? Study help unanswered threads

Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.