Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer division algorithm

Tags:

People also ask

What is the division algorithm formula?

The division algorithm formula is: Dividend = (Divisor × Quotient) + Remainder. This can also be written as: p(x) = q(x) × g(x) + r(x), where, p(x) is the dividend. q(x) is the quotient.

What is division algorithm of whole numbers?

(Division Algorithm) If a whole number a is divided by a non-zero whole number b, then there exists whole numbers q and r such that a = bq + r, where either r = 0 or, r < b.

What is an example of integer division?

Division of integers means equal grouping or dividing an integer into a specific number of groups. For example, -6 ÷ 2 means dividing -6 into 2 equal parts, which results in -3.


I was thinking about an algorithm in division of large numbers: dividing with remainder bigint C by bigint D, where we know the representation of C in base b, and D is of form b^k-1. It's probably the easiest to show it on an example. Let's try dividing C=21979182173 by D=999.

  • We write the number as sets of three digits: 21 979 182 173
  • We take sums (modulo 999) of consecutive sets, starting from the left: 21 001 183 356
  • We add 1 to those sets preceding the ones where we "went over 999": 22 001 183 356

Indeed, 21979182173/999=22001183 and remainder 356.

I've calculated the complexity and, if I'm not mistaken, the algorithm should work in O(n), n being the number of digits of C in base b representation. I've also done a very crude and unoptimized version of the algorithm (only for b=10) in C++, tested it against GMP's general integer division algorithm and it really does seem to fare better than GMP. I couldn't find anything like this implemented anywhere I looked, so I had to resort to testing it against general division.

I found several articles which discuss what seem to be quite similar matters, but none of them concentrate on actual implementations, especially in bases different than 2. I suppose that's because of the way numbers are internally stored, although the mentioned algorithm seems useful for, say, b=10, even taking that into account. I also tried contacting some other people, but, again, to no avail.

Thus, my question would be: is there an article or a book or something where the aforementioned algorithm is described, possibly discussing the implementations? If not, would it make sense for me to try and implement and test such an algorithm in, say, C/C++ or is this algorithm somehow inherently bad?

Also, I'm not a programmer and while I'm reasonably OK at programming, I admittedly don't have much knowledge of computer "internals". Thus, pardon my ignorance - it's highly possible there are one or more very stupid things in this post. Sorry once again.

Thanks a lot!


Further clarification of points raised in the comments/answers:

Thanks, everyone - as I didn't want to comment on all the great answers and advice with the same thing, I'd just like to address one point a lot of you touched on.

I am fully aware that working in bases 2^n is, generally speaking, clearly the most efficient way of doing things. Pretty much all bigint libraries use 2^32 or whatever. However, what if (and, I emphasize, it would be useful only for this particular algorithm!) we implement bigints as an array of digits in base b? Of course, we require b here to be "reasonable": b=10, the most natural case, seems reasonable enough. I know it's more or less inefficient both considering memory and time, taking into account how numbers are internally stored, but I have been able to, if my (basic and possibly somehow flawed) tests are correct, produce results faster than GMP's general division, which would give sense to implementing such an algorithm.

Ninefingers notices I'd have to use in that case an expensive modulo operation. I hope not: I can see if old+new crossed, say, 999, just by looking at the number of digits of old+new+1. If it has 4 digits, we're done. Even more, since old<999 and new<=999, we know that if old+new+1 has 4 digits (it can't have more), then, (old+new)%999 equals deleting the leftmost digit of (old+new+1), which I presume we can do cheaply.

Of course, I'm not disputing obvious limitations of this algorithm nor I claim it can't be improved - it can only divide with a certain class of numbers and we have to a priori know the representation of dividend in base b. However, for b=10, for instance, the latter seems natural.

Now, say we have implemented bignums as I outlined above. Say C=(a_1a_2...a_n) in base b and D=b^k-1. The algorithm (which could be probably much more optimized) would go like this. I hope there aren't many typos.

  • if k>n, we're obviously done
  • add a zero (i.e. a_0=0) at the beginning of C (just in case we try to divide, say, 9999 with 99)
  • l=n%k (mod for "regular" integers - shouldn't be too expensive)
  • old=(a_0...a_l) (the first set of digits, possibly with less than k digits)
  • for (i=l+1; i < n; i=i+k) (We will have floor(n/k) or so iterations)
    • new=(a_i...a_(i+k-1))
    • new=new+old (this is bigint addition, thus O(k))
    • aux=new+1 (again, bigint addition - O(k) - which I'm not happy about)
    • if aux has more than k digits
      • delete first digit of aux
      • old=old+1 (bigint addition once again)
      • fill old with zeroes at the beginning so it has as much digits as it should
      • (a_(i-k)...a_(i-1))=old (if i=l+1, (a _ 0...a _ l)=old)
      • new=aux
    • fill new with zeroes at the beginning so it has as much digits as it should
    • (a_i...a_(i+k-1)=new
  • quot=(a_0...a_(n-k+1))
  • rem=new

There, thanks for discussing this with me - as I said, this does seem to me to be an interesting "special case" algorithm to try to implement, test and discuss, if nobody sees any fatal flaws in it. If it's something not widely discussed so far, even better. Please, let me know what you think. Sorry about the long post.

Also, just a few more personal comments:

@Ninefingers: I actually have some (very basic!) knowledge of how GMP works, what it does and of general bigint division algorithms, so I was able to understand much of your argument. I'm also aware GMP is highly optimized and in a way customizes itself for different platforms, so I'm certainly not trying to "beat it" in general - that seems as much fruitful as attacking a tank with a pointed stick. However, that's not the idea of this algorithm - it works in very special cases (which GMP does not appear to cover). On an unrelated note, are you sure general divisions are done in O(n)? The most I've seen done is M(n). (And that can, if I understand correctly, in practice (Schönhage–Strassen etc.) not reach O(n). Fürer's algorithm, which still doesn't reach O(n), is, if I'm correct, almost purely theoretical.)

@Avi Berger: This doesn't actually seem to be exactly the same as "casting out nines", although the idea is similar. However, the aforementioned algorithm should work all the time, if I'm not mistaken.