# 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
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://cboard.cprogramming.com/cplusplus-programming/51419-help-fraction-problem-printable-thread.html", "fetch_time": 1477159305000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2016-44/segments/1476988719033.33/warc/CC-MAIN-20161020183839-00297-ip-10-171-6-4.ec2.internal.warc.gz", "warc_record_offset": 42265032, "warc_record_length": 3930, "token_count": 912, "char_count": 3589, "score": 3.53125, "int_score": 4, "crawl": "CC-MAIN-2016-44", "snapshot_type": "latest", "language": "en", "language_score": 0.8166062831878662, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00050-of-00064.parquet"}