We now consider division with remainder. We want to compute
q
and
r
such that
a
=
bq
+
r
and 0
≤
r < b
. Let us assume that
a
≥
b
; otherwise, we can just set
q
= 0 and
r
=
a
. Also, let
us assume that
b
‘

1
6
= 0. The quotient
q
will have at most
m
=
k

‘
+ 1 base
B
digits. Write
q
= (
q
m

1
· · ·
q
0
)
B
. We compute the digits of
q
and the value
r
with the following division with
remainder algorithm.
12
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
r
←
a
for
i
←
m

1 down to 0 do
q
i
← b
r/B
i
b
c
r
←
r

B
i
·
q
i
b
To verify that this procedure is correct, one easily verifies by induction that in each loop
iteration,
r < B
i
+1
b
.
It is perhaps not immediately clear how to efficiently implement the step
q
i
← b
r/B
i
b
c
. As in
the paperandpencil one has to make a reasonable “guess” at
q
i
, and then correct the guess if it
turns out to be wrong.
More generally, consider the following situation. Let
x
and
y
be positive integers with
x/y < B
,
and let
d
=
b
x/y
c
. Suppose
B
= 2
t
and that
y
is has
n
+
t
bits in its binary representation, where
n
≥
0. Then we can write
y
= ˆ
y
2
n
+
y
0
, where 2
t

1
≤
ˆ
y <
2
t
and 0
≤
y
0
<
2
n
. We can also write
x
= ˆ
x
2
n
+
x
0
, where 0
≤
ˆ
x <
2
2
t
and 0
≤
x
0
<
2
n
. Then we can approximate
d
by
ˆ
d
=
b
ˆ
x/
ˆ
y
c
.
Theorem 3.1
With notation as in the previous paragraph, we have
d
≤
ˆ
d
≤
d
+ 2
.
Proof.
To prove the first inequality, it suffices to show that
x

ˆ
dy < y
.
Using the fact that
ˆ
x
=
ˆ
d
ˆ
y
+ ˆ
z
, where 0
≤
ˆ
z <
ˆ
y
, we have
x

ˆ
dy
≤
x

ˆ
d
ˆ
y
2
n
≤
x

(ˆ
x

(ˆ
y

1))2
n
=
x

ˆ
x
2
n
+ (ˆ
y

1)2
n
<
2
n
+ (ˆ
y

1)2
n
= ˆ
y
2
n
≤
y.
That proves the first inequality.
To prove the second inequality, it suffices to show that
x

ˆ
dy
≥ 
2
y
. We have
x

ˆ
dy
≥
x

ˆ
d
(ˆ
y
2
n
+ 2
n
) =
x

ˆ
d
ˆ
y
2
n

ˆ
d
2
n
≥
x

ˆ
x
2
n

ˆ
d
2
n
≥ 
ˆ
d
2
n
.
So it suffices to show that
ˆ
d
2
n
/y
≤
2. Using the fact that ˆ
y
≥
2
t

1
, we have
ˆ
d
2
n
y
≤
ˆ
x
2
n
ˆ
yy
≤
x
ˆ
yy
≤
2
t
ˆ
y
≤
2
t
2
t

1
= 2
.
That proves the second inequality.
2
Now, going back to our division with remainder algorithm, consider executing one iteration of
the main loop. If
i
= 0 and
b < B
, then we can compute
b
r/b
c
in a single step, using the “builtin”
division instruction; otherwise, we apply the above theorem with
y
:=
B
i
b
≥
B
and
x
:=
r < Bx
.
We can extract the highorder
t
bits from
y
and the corresponding highorder bits of
x
, and with
one division of a number with less than 2
t
bits by a
t
bit number, we get an approximation ˆ
q
i
to
q
i
.
All of this can be carried out in time
O
(1). With the above theorem,
q
i
≤
ˆ
q
i
≤
q
i
+ 2. We perform
the subtraction step
r
←
r

B
i
·
ˆ
q
i
b
, which takes time
O
(
‘
). At this point, we can easily detect if
our approximation was too large. Correcting the values of ˆ
q
i
and
r
then can be done in time
O
(
‘
).
Thus, each loop iteration takes time
O
(
‘
), and hence the total runningtime of this division with
remainder algorithm is
O
(
m‘
).
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '13
 MRR
 Math, Algebra, Number Theory

Click to edit the document details