# Help with fraction problem

Printable View

• 04-04-2004
jrahhali
Help with fraction problem
Hello. I'm trying to put a fraction in lowest terms, but i can't seem to get it working; I work it out on paper, everything seems it should be ok.
NOTE: den is for denominator, num is for numerator, gcd is for greatest common divisor. I start out with the number 2 for gcd and then test if that number can divide into the numerator and denominator evenly. If not, gcd is increased by one until it reaches den/2. if it does divide evenly into both, then num and den are both diveded by gcd and gcd is set to 1, whereon looping, setting gcd = 2, and repeating the process over agian.

Here's the code:
Code:

```void Fraction::lowterms() {         for(int gcd = 2; gcd <= den/2; gcd++ )                 while(!(num%gcd) && !(den%gcd) )                 {                         num = num / gcd;                         den = den / gcd;                         gcd = 1;                 } }```
It's really strange.
1) Sometimes the nothing is displayed on the screen ( i presume it's going into an infitite loops )

2) the numerator equates to some super high number like 185764 and the denominator is correct

3) the numerator equates to some super high number liek 185764 and the denominator is wrong ( but a low number );

i can't seem to figure out why... :confused:
• 04-04-2004
jverkoey
Quote:

Originally Posted by jrahhali
Code:

```void Fraction::lowterms() {         for(int gcd = 2; gcd <= den/2; gcd++ )                 while(!(num%gcd) && !(den%gcd) )                 {                         num = num / gcd;                         den = den / gcd;                         gcd = 1;                 } }```

try fixing how far you go in your for loop...I think i have some working code, but i'd like to see if you can figure it out :) if you are really stuck, just PM me and i'll tell you what i did, it's actually pretty simple, so it shouldn't be too hard

-edit-
also, about the infinite loop, notice that when you modulus a number by one, it will never have a remainder, so you end up dividing by one, over and over and over in the while loop
• 04-04-2004
jrahhali
>>I think i have some working code
?? can i see it? hehe.

>>notice that when you modulus a number by one...
i actually set gcd to one so that when control goes to the for loop again gcd is incremented, and making it have a value of 2, so 1 is never used for the modulous division.
• 04-04-2004
jverkoey
well, in your while loop, you set the gcd to one once it finds a common denominator. But then right after that you test the denominators again, and the remainder of any number divided by 1 is going to be 0. So unless you break from the while loop, it's just going to go forever

also, in my code, i made it loop until it hit either the numerator or the denominator. I think it's best to choose the one that's lowest and go until that one

Code:

```void Fraction::lowterms() {         int countTo=den;         if(num<den)                 countTo=num;         for(int gcd = 2; gcd <= countTo; gcd++ )                 while(!(num%gcd) && !(den%gcd) )                 {                         num = num / gcd;                         den = den / gcd;                         gcd = 1;                         break;                 } }```
• 04-04-2004
jrahhali
>> But then right after that you test the denominators again, and the remainder of any number divided by 1 is going to be 0
:D great great.. thanks alot.. iot must have slipped my mind that the WHILE LOOPS ACTUALLY CONTINUES if it's true. hehe .. cheers