Results 1 to 12 of 12
  1. #1
    jayden is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default recursive function

    can anyone convert this functional paradigm algorithim to a recursive algorithim thanks,

    BEGIN program(num1, num2)
    declare num3 as integer
    WHILE num2 !=0
    set num3 to the remainder of num1/num2
    set num1 to num2
    set num2 to num3
    ENDWHILE
    RETURN num1
    END program

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jayden View Post
    can anyone convert this functional paradigm algorithim to a recursive algorithim thanks,
    Yes I can and I'm sure others can do it too but can you do it? This is not a rent-a-coder forum; you show your attempts and tell us where you are stuck with it.

    kind regards,

    Jos

  3. #3
    jayden is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    i got as far as writing a recursive function that is extremly simple
    BEGIN count(num)
    IF num<3
    print num
    count (n+1)
    END IF
    END Count
    but i cant seem to adapt the aspects of this algorithim into it any help/ pointers would be great

  4. #4
    jayden is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    it would be greatly appreciated if you could step me through the process of changing the algorithim to a recursive function. iam new to software development so please bare with me

  5. #5
    jayden is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    is their anyone who can help me out all i need is two or three lines of code and my problem is solved, please help me out

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jayden View Post
    can anyone convert this functional paradigm algorithim to a recursive algorithim thanks,

    BEGIN program(num1, num2)
    declare num3 as integer
    WHILE num2 !=0
    set num3 to the remainder of num1/num2
    set num1 to num2
    set num2 to num3
    ENDWHILE
    RETURN num1
    END program
    You recursive method should have a structure like this:

    Java Code:
    BEGIN program(num1, num2)
    IF (num2 == 0) RETURN num1
    return program( ... , ...)
    Notice the recursive call at the end of the function body; you should pass the correct parameter values (those can be found/deduced from the original WHILE body).

    kind regards,

    Jos

  7. #7
    jayden is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    thank you very much appreciated

  8. #8
    jayden is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    BEGIN program (num1, num2)
    declare num3 as int
    IF(num2 == 0) RETURN num1
    return program(num1/num2) to num3
    (num1 to num2)
    (num2 to num3)
    ENDIF
    Return num1
    END program

    hopefully this will work if not give me one more pointer in the right direction:)
    cheers jayden

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jayden View Post
    BEGIN program (num1, num2)
    declare num3 as int
    IF(num2 == 0) RETURN num1
    return program(num1/num2) to num3
    (num1 to num2)
    (num2 to num3)
    ENDIF
    Return num1
    END program

    hopefully this will work if not give me one more pointer in the right direction:)
    cheers jayden
    We are talking all pseudo code but I'd rather say it'd be:

    Java Code:
    BEGIN program(num1, num2)
    IF (num2 == 0) RETURN num1
    return program( num2, remainder of num1 and num2)
    END program
    kind regards,

    Jos

  10. #10
    jayden is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    what happened to num 3

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jayden View Post
    what happened to num 3
    You want it back? Here it is back again:

    Java Code:
    BEGIN program(num1, num2)
    IF (num2 == 0) RETURN num1
    DECLARE num3 as int
    num3= remainder of num1 and num2
    return program(num2, num3)
    END program
    kind regards,

    Jos

  12. #12
    jayden is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    that you again Jos your efforts are greatly appreciated
    cheers Jayden

Similar Threads

  1. basic help with Recursive
    By syntrax in forum New To Java
    Replies: 3
    Last Post: 12-15-2009, 06:19 AM
  2. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  3. Recursive Counting
    By zlwilly in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 08:42 PM
  4. Understanding this recursive function
    By LifeWithJava in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:26 AM
  5. Help with recursive function in java
    By cachi in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •