Latest Article

Its no secret that when people contradict themselves, it has the effect of making the flaws in their actions or statements seem glaringly obvious. But what about when WE ourselves get caught contradicting ourselves by someone else?

 By: Nick Tumminello Added: January 6th, 2014
More Recent Articles
 Contrast Training for Size By: Lee Boyce
 An Interview with Marianne Kane of Girls Gone Strong By: Jordan Syatt
 What Supplements Should I be Taking? By: Jay Wainwright
 Bench Like a Girl By: Julia Ladewski
 Some Thoughts on Building a Big Pull By: Christopher Mason

1. ## 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);
}

2. 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.

3. 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. oh yeah that definately gets me started thanks

5. I managed to get kicked out of my java class.

I've been enjoying my free time lately

6. 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!

7. 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.

8. 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.

9. sitepoint.com is a pretty good forum for this type of thing, might want to check it out!

10. 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.

#### Posting Permissions

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