Hey there! Sign in to join this conversationNew here? Join for free
    Offline

    21
    ReputationRep:
    (Original post by will'o'wisp2)
    https://cdn.discordapp.com/attachmen...88/unknown.png

    i've done this but i don't think it's recursion .__.
    You're right. That's not recursion.

    There is not a single a line of code that makes the function call itself.

    Once you've done this, I suggest trying to learn Haskell. That will force you to do/learn recursion properly.
    • TSR Support Team
    • Study Helper
    Offline

    20
    ReputationRep:
    (Original post by will'o'wisp2)
    but no i want it to multiply by the previuos number so if i stick 5 in i want 5x4 to come out

    then again i'm not sure how i'd do recursion with any 2 numbers
    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.
    • Thread Starter
    Online

    14
    ReputationRep:
    (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
    • TSR Support Team
    • Study Helper
    Offline

    20
    ReputationRep:
    (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
    • TSR Support Team
    • Study Helper
    Offline

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

    21
    ReputationRep:
    (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.
    • TSR Support Team
    • Study Helper
    Offline

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

    21
    ReputationRep:
    (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
    • Thread Starter
    Online

    14
    ReputationRep:
    (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
    • TSR Support Team
    • Study Helper
    Offline

    20
    ReputationRep:
    (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.
    • Thread Starter
    Online

    14
    ReputationRep:
    (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
    • Thread Starter
    Online

    14
    ReputationRep:
    (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?
    • TSR Support Team
    • Study Helper
    Offline

    20
    ReputationRep:
    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).
    • Thread Starter
    Online

    14
    ReputationRep:
    (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,....
    • Thread Starter
    Online

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

    21
    ReputationRep:
    (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
    • Thread Starter
    Online

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

    21
    ReputationRep:
    (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.
    • TSR Support Team
    • Study Helper
    Offline

    20
    ReputationRep:
    (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.

    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.
    • Thread Starter
    Online

    14
    ReputationRep:
    (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))
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Did TEF Bronze Award affect your UCAS choices?
    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
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • 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

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