# programming help

• 03-26-2008, 08:43 PM
beaverfootball
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);
}
• 03-26-2008, 09:00 PM
noahfor123
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.
• 03-26-2008, 09:00 PM
Googled for a bit, found the formula and coded it up with some base cases:

Quote:

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. :(
• 03-26-2008, 09:04 PM
beaverfootball
oh yeah that definately gets me started :) thanks
• 03-26-2008, 09:07 PM
jAy_Dub
I managed to get kicked out of my java class.

I've been enjoying my free time lately :)
• 03-26-2008, 10:26 PM
deeder
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!
• 03-27-2008, 12:26 AM
lakeripple
Quote:

Originally Posted by deeder
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.
• 03-27-2008, 12:41 AM
Quote:

Originally Posted by lakeripple
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. :)
• 03-27-2008, 04:45 AM
Joe Black
sitepoint.com is a pretty good forum for this type of thing, might want to check it out!
• 03-27-2008, 07:32 AM
RedSpikeyThing
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.