Results 1 to 6 of 6

Thread: C++ Algorithm

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2000
    Posts
    62

    C++ Algorithm

    How do you find the least common denominator for a fraction?
    I am using a class to create a custom data class for fractions. It will be implemented with a class "Fraction." The data will be two integers, numerator and denominator.
    The problem is, I have to be able to reduce each fraction. For instance, 8/2 should equal 4/1 and 2/8 should equal 1/4. I was told that if you took either number, divided it by the other ( like 127/9 ) and kept divideding then the divisor by the remainder, you would come up with the LCM. Is this true? how do you implement this?
    "As for me, I am poor and needy but God is thinking about me right now." Psalm 40

  2. #2
    Zaei
    Guest
    Code:
    while((num%2 == 0)) && (dem%2 == 0))
    {
       num = num / 2; dem = dem / 2;
    }
    Should work, I just came up with it in my head... Just says that if both the numerator and denominator are even, divide both by 2.

    Z.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2000
    Posts
    62
    What if they are odd?
    "As for me, I am poor and needy but God is thinking about me right now." Psalm 40

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I don't know if the remainders method you mentioned would work, but I guess it might *Shrug*

    Intuitively, it seems that the logical way to do this is to find the highest common factor (HCF) of the two numbers and divide them both by this, and keep finding the HCF and dividing until the HCF is 1.

    Finding the HCF of two integers can be done algorithmically by repeatedly subtracting the lesser from the greater until they are equal. When they are equal to each other, they are also equal to the HCF of the original two integers. For example, to find the HCF of 30 and 9:

    30 ... 9 (start)
    21 ... 9 (subtract 9 from 30)
    12 ... 9 (subtract 9 from 21)
    3 ..... 9 (subtract 9 from 12)
    3 ..... 6 (subtract 3 from 9)
    3 ..... 3 (subtract 3 from 6)
    They are equal so HCF is 3

    I hope that helps.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Zaei
    Guest
    Ok, lets rewrite...

    Code:
    int max = Max(num, dem);  //function Max(...) returns highest value...
    for(int i = max; i > 0; i--)
    {
      while((num%i == 0)) && (dem%i == 0))
      {
         num = num / i; dem = dem / i;
      }
    }
    This one seems more likely to work... Tell me what you find...

    Z.

    [edit]
    And please try to remember that its almost 1 AM... =)

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2000
    Posts
    62
    I got it. All you do keep dividing by the divisor and using the remainder as the next divisor (well, sort of)

    void Rational::ReduceFraction()
    {
    int remainder;
    int dividend;
    int divisor;

    dividend = abs(numr);
    divisor = abs(denom);

    while(true)
    {
    remainder = dividend % divisor;

    if (remainder == 0)
    break;
    else
    {
    dividend = divisor;
    divisor = remainder;
    }
    }

    numr /= divisor;
    denom /= divisor;

    }
    "As for me, I am poor and needy but God is thinking about me right now." Psalm 40

Posting Permissions

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



Click Here to Expand Forum to Full Width