23

I'm currently making my own BigInt class, by splitting numbers by 7 digits. (i.e. in base 10,000,000)

I implemented addition, subtraction, and multiplication, and now I'm implementing division and mod. I wrote a code that performs division by long division (estimating numbers by dividing most-significant digits), and it works.

However, it is too slow. When I test operations on a 108-digit number and a 67-digit number, it takes 1.9ms to calculate division, much slower than other operations (0.007~0.008ms to calculate addition/subtraction, 0.1ms to calculate multiplication).

Like Karatsuba and FFT algorithm for fast multiplication, what algorithms exist for calculating division? Wikipedia demonstrates some division algorithm (which calculates multiplicative inverse of divisor and multiplies it with dividend), but I think that doesn't help me much implementing division. I read the 'Large Integer Methods' sections too but that doesn't help me neither... :(

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
JiminP
  • 2,136
  • 19
  • 26
  • 3
    Good question, but you might consider http://programmers.stackexchange.com instead of stackoverflow. – Ben Lee Jan 16 '12 at 17:11
  • Simple google search demonstrates tones of article about this, I don't think it's easy work, but the main Idea is use fast multiplication, and all the algorithms are around this. e.g you can take a look at [fast division in large integers](http://www.treskal.com/kalle/exjobb/original-report.pdf) – Saeed Amiri Jan 16 '12 at 17:27
  • Any chance you could open source this? There isn't currently a good bigint library for JS. – Nico Burns Jan 16 '12 at 20:39

3 Answers3

3

The standard reference for big-integer arithmetic is Donald Knuth's book Art of Computer Programming, Volume 2, Section 4.3. His division algorithm is basically the grade-school algorithm, with some small improvements.

By the way, most people that implement big-integer arithmetic use a power of two rather than a power of ten as the radix of their number system.

user448810
  • 17,381
  • 4
  • 34
  • 59
2

I'd suggest you have a look at the source code of the GMP library and port the functionality you need to JavaScript, or get an idea of how it's done.

If there is a good algorithm out there, this library will most likely have it; and it is distributed under the LGPL.

copy
  • 3,301
  • 2
  • 29
  • 36
  • You can also use the LLVM -> javascript (emscripten) compiler to automatically port the entire library – Raynos Jan 16 '12 at 17:38
  • @Raynos I wouldn't bet on the output of emscripten being callable from JS without major pain, and especially not across emscription versions. –  Jan 16 '12 at 17:46
  • I read some codes (`divexact.c` and `divegcd.c`), and it seems that bitwise operations are included, which I can't implement (because I'm representing numbers in base 10^7)... – JiminP Jan 17 '12 at 10:26
1

For a reasonably fast division algorithm, look at http://myweb.lmu.edu/dmsmith/MComp1996.pdf

It is still O(n^2) but efficient for moderate lengths. It is especially well suited when you are using bases that are smaller than the word size. Years ago, I implemented it in Python. The code is buried in http://home.comcast.net/~casevh/decint_041.tar.gz . Look for the functions smithdiv() and remainder_norm().

casevh
  • 11,093
  • 1
  • 24
  • 35