
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(n1);
}
Starting over...
You get out what you put in.
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; 03262008 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 warmup.
Googled for a bit, found the formula and coded it up with some base cases:
I hope you can read the code without the tabbing... there isn't a [code] tag on these forums.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(m1, n)+n;
}
public static void main(String[] args)
{
Recursive c = new Recursive();
System.out.println(c.multiply(3,6));
}
}
oh yeah that definately gets me started thanks
Starting over...
You get out what you put in.
I managed to get kicked out of my java class.
I've been enjoying my free time lately
int mult(int a,int b)
{
if (b < 1  a == 0)
return 0;
else
return(a + mult(a,b1));
}
That should do the trick!
Last edited by deeder; 03262008 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)
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  HCT12  If you want big gains in size and strength, huge decreases in body fat, or both  check out HCT12.
Can I have some lean muscle & strength please? My Training Journal
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 + (a1) * 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; 03272008 at 07:32 AM.
Bookmarks