Thread: Long Division

  1. #1
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808

    Long Division

    Write a program to output long division of two integers.
    Extra points for multiple bases.
    Example run:
    Code:
    > longdiv 12345 21
    
          587
       ------
    21 )12345
        105
        ---
         184
         168
         ---
          165
          147
          --- 
           18

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    No, that's not how it works sorry.
    You post the code you have so far, and tell us what bit you're having trouble with.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    So far I have:-

    Code:
    main(){
    
    }
    Anyone got any ideas?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes - read FAQ
    it should be
    Code:
    int main(void)
    {
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    If you're in need of a starting point, then check out CRC algorithms by googling etc. They're really nothing more than long division in disguise.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    No, that's not how it works sorry.
    You post the code you have so far, and tell us what bit you're having trouble with.
    Sorry, I didn't make myself clear.
    I'm not a student and it's not a homework problem.
    It's a challenge.
    Here's a starting point:
    Code:
    void longdiv( char* sDividend, char* sDivisor ) { /* sDividend / sDivisor */
    }
    
    int main( int argc, char** argv ) {
        if (argc < 3) return 1;
        longdiv( argv[ 1 ], argv[ 2 ]);
    }

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well I am able to look at that problem description and know how to do it, so I'm not going to bother. Afterall, I had to work it out for base 2 once.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I saw another post suggesting a different challenge and also a post on roman numerals, but they could have been in a different forum.
    So I kind of "made up" the long division one. I didn't realize it was a standard programming puzzle. It just seemed funny to me to make the computer do long division! The binary idea sounds good. I'll try it that way. Thanks.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's not so much a common programming puzzle, as it's a common necessity for any big-integer number class, whether you implement it as a string or in binary.

    Anyway, sorry about my earlier response. It seems obvious now that you really were just posting a puzzle, rather than posting your own homework, and later pretending it was a puzzle for others. People often do that you see.

    The binary division case is actually particularly easy because you can eliminate any traces of multiplication from the function since things either get multiplied by zero or one. But yeah it is worth playing around to implment the version that works on various bases (2 to 10 say).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    ...posting your own homework, and later pretending it was a puzzle for others. People often do that you see.
    No problem. I've seen quite a bit of that now that I've browsed around
    a little more. I would mention to people asking for help that they should
    generally put a little more effort into wording their questions if they
    expect others to put in the effort to answer them.

    Quote Originally Posted by iMalc View Post
    ... it's a common necessity for any big-integer number class ... But yeah it is worth playing around to implment the version that works on various bases (2 to 10 say).
    I see what you mean about big numbers. I hadn't realized the connection,
    but now that you mention it I can see the pattern of a general solution,
    a much cleaner pattern than I had conceived.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in Converting Unsigned long to String
    By cprogrammer_18 in forum C Programming
    Replies: 8
    Last Post: 01-14-2006, 08:57 AM
  2. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  3. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. BigInt Linked List Long Division
    By abyssknight in forum C Programming
    Replies: 5
    Last Post: 04-01-2004, 07:02 PM