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
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
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"
So far I have:-
Anyone got any ideas?Code:main(){ }
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
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.
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 ]); }
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"
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.
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"
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.
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.