Question: Integer Division

Hi,

I had an issue where I wanted the quotient of two integers a and b; the quotient q from the division algorithm for a/b which finds a = b*q + r, 0 <= r < b

I was not sure how to obtain it. Since,

a := 7;
b := 3;
q := a / b; # returns fraction 7/3

Received some help from G. A. Edgar from Ohio State and Joe Riel from Maple; who clued me into the use of 'iquo' to obtain this.

a := 7;
b := 3;
q := iquo(a,b,'r'); # q = 2, r = 1

I was experimenting with floor(a/b), but I am sure this is more efficient (no conversion between fraction and integer);

There is 'irem' which just determines the remainder from integer division. The 3rd argument in iquo is optional ... and returns the remainder from the division.

This may not be the q from the division algorithm if a is negative. I noticed:

q := iquo(-1,3,'y'); # q = 0, r = -1

Rather than q = -1, r = 2, leaving r non-negative as I had expected (0 <= r < b)

My documentation (Maple 9) says:
"if m and n are integers then irem returns r such that m = n*q + r, abs(r) < abs(n) and m*r >= 0"

so ...

-1 = 3*0 + -1, abs(-1) < abs(3) and -3*-1 >= 0

so, remainder has to be negative for that to hold in this example.

whereas

-1 mod 3 # returns 2

So, the mod function appears to follow the division algorithm ... where iquo may not.

-Jay
Please Wait...