Results 1 to 10 of 10

Thread: programming help

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jul 2007
    Posts
    250

    programming help

    hey guys.. we are going over recursion in java. and even if you don't know much computer science or java you can pry still help a little with the actual concept.

    alright ---- i have to make a procedure called multiply, and i pass it 2 variables int a, int b

    so for instance , i call mulitply(3, 6) and i need to get 18. But i have to use recursion to get it.. or in other words i can't just say 3 times 6 = 18.



    another example of recursion is the factorial function.. i understood that one just fine.

    this is the factorial procedure and sort of what it needs to look like
    if(n == 1)
    {
    return 1;
    }
    else
    {
    return n * factorial(n-1);
    }
    Starting over...

    You get out what you put in.

  2. #2
    Senior Member noahfor123's Avatar
    Join Date
    May 2002
    Location
    Pittsburgh, PA
    Posts
    507
    So, whenever you have a recursive method there is going to be something done over and over. Can you think of a way to do multiplication that involves doing something over and over? Think about the arguments that are being passed to the function and how they might change with each recursive function call. Think about the base case.
    Last edited by noahfor123; 03-26-2008 at 09:02 PM.
    The Iron never lies to you. The Iron will always kick you the real deal. It never freaks out on me, never runs. Friends may come and go, but 200 pounds is always 200 pounds - a warm-up.

  3. #3
    Senior Member kad's Avatar
    Join Date
    May 2005
    Posts
    824
    Googled for a bit, found the formula and coded it up with some base cases:

    class Recursive {
    int multiply(int m, int n)
    {
    if (m == 0 || n == 0) {
    return 0;
    }

    if (m == 1) {
    return n;
    }

    if (n == 1) {
    return m;
    }

    /* only works for m > 0 */
    return multiply(m-1, n)+n;

    }

    public static void main(String[] args)
    {
    Recursive c = new Recursive();
    System.out.println(c.multiply(3,6));
    }
    }
    I hope you can read the code without the tabbing... there isn't a [code] tag on these forums.

  4. #4
    Senior Member
    Join Date
    Jul 2007
    Posts
    250
    oh yeah that definately gets me started thanks
    Starting over...

    You get out what you put in.

  5. #5
    Determined jAy_Dub's Avatar
    Join Date
    Jun 2004
    Posts
    0
    I managed to get kicked out of my java class.

    I've been enjoying my free time lately


    AtLarge Nutrition Supplements Get the best supplements and help support Wannabebig![/font][/size]

  6. #6
    Senior Member deeder's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    2,255
    int mult(int a,int b)
    {

    if (b < 1 || a == 0)
    return 0;
    else
    return(a + mult(a,b-1));

    }


    That should do the trick!
    Last edited by deeder; 03-26-2008 at 10:33 PM.
    Full Powerlifting
    Squat - 595lbs -- 270kg -- Dec. 31, '09 (Provincial Record @100kg class)
    Bench - 374lbs -- 170kg -- Dec 20, '08 (@100kg class)
    Dead - 589lbs -- 267.5kg -- Dec 20, '08 (Provincial Record @100kg class)
    Total: 1537lbs -- 697.5kg -- Dec 20, '08 (Provincial Record @ 100kg class)
    Bench Only -- 358lbs -- 162.5kg -- Nov. 25, '07 (Provincial Record @ 90kg class)
    Bench Only -- 376lbs -- 171kg -- Jan. 26, '08 (Provincial Record @ 100kg class)

  7. #7
    Senior Member lakeripple's Avatar
    Join Date
    Dec 2006
    Posts
    195
    Quote Originally Posted by deeder View Post
    int mult(int a,int b)
    {

    if (b < 1 || a == 0)
    return 0;
    else
    return(a + mult(a,b-1));

    }


    That should do the trick!
    It's a good algorithm, but it's flawed in the same way as the first one, it doesn't take into consideration negative numbers. To the OP, use this algorithm, and modify a copy for negative number cases.
    Goal: Set a deadlift provincial record in mens junior.

  8. #8
    Senior Member kad's Avatar
    Join Date
    May 2005
    Posts
    824
    Quote Originally Posted by lakeripple View Post
    It's a good algorithm, but it's flawed in the same way as the first one, it doesn't take into consideration negative numbers. To the OP, use this algorithm, and modify a copy for negative number cases.
    Yeah, that's why I added the comment above the recursive statement that it only worked for positive numbers. It was just meant to get the OP started in the right direction.

  9. #9
    Soon to be lean... Joe Black's Avatar
    Join Date
    Dec 2000
    Location
    Cambridge, England
    Posts
    10,936
    sitepoint.com is a pretty good forum for this type of thing, might want to check it out!
    http://www.wannabebig.com/logo/alnlogo_black.gif

    AtLarge Nutrition Supplements Get the best supplements and help support Wannabebig!

    Hypertrophy Cluster Training - HCT-12 - If you want big gains in size and strength, huge decreases in body fat, or both - check out HCT-12.

    Can I have some lean muscle & strength please? My Training Journal

  10. #10
    Banned
    Join Date
    Dec 2005
    Location
    Toronto, Ontario
    Posts
    4,548
    Recursion is not an easy concept to wrap your head around, so don't be surprised if it takes a while to get.

    There are two important steps to writing something recursively:
    1) Find a base case. This is something that is trivially true. So, for example, multiplying something by 1.

    2) Find the recursion. This is the hard part. Find a way to break the problem into a smaller version of the same problem. For your multiplication problem, a * b = a + (a-1) * b

    Handling negative numbers is a special case of step 2. How can you break it into a similar version of the same problem? Well, if you know that a is negative, then a * b = - (|a| * b).

    Feel free to PM me if you need some more help.
    Last edited by RedSpikeyThing; 03-27-2008 at 07:32 AM.

Similar Threads

  1. Website Programming
    By d'Anconia in forum General Chat
    Replies: 7
    Last Post: 10-21-2007, 05:12 PM
  2. practical programming - texas method - routine help
    By noahfor123 in forum Bodybuilding & Weight Training
    Replies: 6
    Last Post: 06-27-2007, 11:46 AM
  3. Practical Programming
    By Vapour Trails in forum Bodybuilding & Weight Training
    Replies: 15
    Last Post: 05-30-2007, 11:51 AM
  4. What programming language to learn?
    By BG5150 in forum General Chat
    Replies: 25
    Last Post: 07-19-2006, 07:19 AM
  5. Interested in Programming?
    By defcon in forum General Chat
    Replies: 11
    Last Post: 05-02-2005, 09:36 AM

Bookmarks

Posting Permissions

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