Thread: fractions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    given a fraction n/d you can effectively do long division like this:
    Code:
      value in before decimal = n/d (integer arithmetic)
      remainder before decimal = n % d;
      1st value after decimal = (10 * remainder before decimal) / d
      1st remainder after decimal = (10 * remainder before decimal) % d
      2nd value after decimal = (10 * 1st remainder after decimal) / d
      2nd remainder after decimal = (10 * 1st remainder after decimal) % d
      ... and so on until either:
        the remainder is 0 - no repeating necessary
      or:
        you repeat a previous remainder - decimal notation has a repetition
    The code to do this is actually not as complex as I at first thought
    - its an interesting problem )
    DavT
    -----------------------------------------------

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by DavT
    given a fraction n/d you can effectively do long division like this:
    Code:
      value in before decimal = n/d (integer arithmetic)
      remainder before decimal = n % d;
      1st value after decimal = (10 * remainder before decimal) / d
      1st remainder after decimal = (10 * remainder before decimal) % d
      2nd value after decimal = (10 * 1st remainder after decimal) / d
      2nd remainder after decimal = (10 * 1st remainder after decimal) % d
      ... and so on until either:
        the remainder is 0 - no repeating necessary
      or:
        you repeat a previous remainder - decimal notation has a repetition
    The code to do this is actually not as complex as I at first thought
    - its an interesting problem )
    That's the ticket! Use custom decimal arithmetic routines for dividing decimal numbers with an arbitrarily large number of digits.

    My thoughts were that if you put the digits as '0' and '1' into an ascii string, then you could use strstr() or some such thing to test for periodicity.

    (Maybe a good exercise for you recursion geeks.)

    After you have identified a candidate, then use some method like the one in the first post by okinrus to recreate the original fraction (assuming that common factors have been removed so that the numerator and denominator are relatively prime). You could do this arithmetic in decimal also, using a custom multiplication and subtraction routines.

    My only problem is knowing how to stop (not how to stop the program, but how to stop thinking of ways to do it, given the good ideas presented by the good people here).

    Regards,

    Dave
    Last edited by Dave Evans; 09-08-2004 at 08:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 05:10 AM
  3. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 04:33 AM
  4. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM