I am stuck in a inifnite loop and i cant get out!

sbud34

Member
Aug 17, 2001
73
0
0
Trying to write Euclid's greatest denominator algorithm using recursion today, and i freaking wrote an infinte loop and i dont know how to fix it ahh!! Whoever invented recursion should be dragged out and beaten endlessly with a bamboo stick :disgust:
 

sbud34

Member
Aug 17, 2001
73
0
0


<< run over it with a car... not beat it with a bamboo stick.. recursion SUCKS.. >>



And stone him to death while we're at it.....
 

sbud34

Member
Aug 17, 2001
73
0
0
Two words: Inifinite loops....
btw I have been sitting here for two hours and still cant figure what's going wrong.....:disgust:
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
Stop when the remainder is zero. Make sure you're passing the correct numbers to the next recursive step.
 

MichaelD

Lifer
Jan 16, 2001
31,528
3
76


<< Euclid's greatest denominator algorithm using recursion today >>



Ya'know, it's crap like this that really pisses me off because it makes me feel STUPID. I mean, holyheadachebatman; WTF is that? The Euclid I know was a Greek philosopher not a mathematician. "Using recursion?" OMG, my head hurts...

Ripped from Dictionary.com

re·cur·sion (r-k&ucirc;rzhn)
n. Mathematics
An expression, such as a polynomial, each term of which is determined by application of a formula to preceding terms.
A formula that generates the successive terms of a recursion.


Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaamyheaddddddddddddddddddddd!

"Didn't you take Calculus, MichaelD? Bwahahaha! Math is not my thing...I'm pitifully stupid at it. I never got past HS Algebra. Really. Now, give me a topic and two minutes to think about it and I can get up in front an auditorium full of people and convince you that I actually know what I'm talking about. English/Vocabularly/Speech/Composition I"m great at..but this stuff...sheesh. Recursion my ass. ;)
 

ThisIsMatt

Banned
Aug 4, 2000
11,820
1
0


<< Two words: Inifinite loops....
btw I have been sitting here for two hours and still cant figure what's going wrong.....:disgust:
>>

You can have infinite loops w/o recursion...
 

sbud34

Member
Aug 17, 2001
73
0
0


<< Stop when the remainder is zero. Make sure you're passing the correct numbers to the next recursive step. >>





I have a sentinel when remainder == 0. And In the debugger i see remainder going to zero, but the program wont freaking stop =(
9 lines of code and i can't figure out what's wrong haha :eek:
 

PliotronX

Diamond Member
Oct 17, 1999
8,883
107
106
When I first saw this thread, I thought of DJ eN's "Life Is A Loop" :)

Good luck d00d
 

sbud34

Member
Aug 17, 2001
73
0
0
public int GCD(int x, int y)
{
int remainder = 0;
remainder = x % y;

while (remainder!=0)
{
GCD(y, remainder);
}
return y;
}

If you can figure what up with the code, i will personally set up a shrine in this 12x10 space for you :)
 

Juniper

Platinum Member
Nov 7, 2001
2,025
1
0
is it that my programming sucks or is it that u did not assign a value for y to return
 

ThisIsMatt

Banned
Aug 4, 2000
11,820
1
0
Sorry for taking so long...

The problem with your solution is that your while loop operates on the previous remainder value. For example, if you start with the two numbers '7' & '6', your first remainder will be 7%6= 1, so it will enter the loop. Now your values being passed are '6' & '1', and their remainder is '0' and that step will not enter the while loop, BUT when it comes back into the previous while loop and resumes that one, the current instance of remainder is still 0.

You need to change your while loop into an if statement.
 

darkshadow1

Senior member
Nov 2, 2000
460
0
0
try replacing while with an if statement

Also, you do need to set something equal to whatever GCD returns...

public int GCD(int x, int y)
{
int remainder = 0;
remainder = x % y;

if (remainder!=0)
{
y = GCD(y, remainder);
}
return y;
}

That should do the trick...i'm mad tired though so maybe not. :p


Bah! I've been beaten to..oh well, I was nice and did the corrections for you...I think...but next time no code for you! :)
 

ThisIsMatt

Banned
Aug 4, 2000
11,820
1
0
oh, and yeah it still doesn't work 100% correctly...for example, plug in 139 & 6...it spits out 6...needs to spit out 1...
 

ElPool

Senior member
Oct 11, 2000
665
0
0


<< Trying to write Euclid's greatest denominator algorithm using recursion today, and i freaking wrote an infinte loop and i dont know how to fix it ahh!! Whoever invented recursion should be dragged out and beaten endlessly with a bamboo stick :disgust: >>





int recursion(){

beat whoever invented recursion with bamboo stick();
recursion();

}
 

ThisIsMatt

Banned
Aug 4, 2000
11,820
1
0
public int GCD(int x, int y)
{
int remainder = x % y;

if (remainder!=0)
{
y = GCD(y, remainder);
}
return y;
}
 

pillage2001

Lifer
Sep 18, 2000
14,038
1
81


<< public int GCD(int x, int y)
{
int remainder = x % y;

if (remainder!=0)
{
y = GCD(y, remainder);
}
return y;
}
>>



WoHoooo......5 million dollar code. :)
 

ThisIsMatt

Banned
Aug 4, 2000
11,820
1
0


<<

<< public int GCD(int x, int y)
{
int remainder = x % y;

if (remainder!=0)
{
y = GCD(y, remainder);
}
return y;
}
>>



WoHoooo......5 million dollar code. :)
>>

Hey, dirty, baby I got your money, doncha worry, said hey, baby I got your money...

you gimme your number
I'll call you up
...
 

sbud34

Member
Aug 17, 2001
73
0
0
matt you now have a personal shrine in my drom room.....send pics over and i'll post them on my wall hah :)
 

sbud34

Member
Aug 17, 2001
73
0
0


<<

<< Trying to write Euclid's greatest denominator algorithm using recursion today, and i freaking wrote an infinte loop and i dont know how to fix it ahh!! Whoever invented recursion should be dragged out and beaten endlessly with a bamboo stick :disgust: >>

int recursion(){ beat whoever invented recursion with bamboo stick(); recursion(); }
>>




ROFLMAO! :):p;)
 

Zach

Diamond Member
Oct 11, 1999
3,400
1
81
Wussies. I just did four weeks of a course using Lisp, with imperitive programming disallowed.