Homepage Solution manuals Ivan Niven An Introduction to the Theory of Numbers Exercise 4.1.33 (There is a carry in the $i$th place iff $\{m/d^{i+1}\}+ \{n/d^{i+1}\} \geq 1$)

Exercise 4.1.33 (There is a carry in the $i$th place iff $\{m/d^{i+1}\}+ \{n/d^{i+1}\} \geq 1$)

Let the positive integers m and n be written in base d , say m = i a i d i and n = j b i d i . Show that when m and n are added, that there is a carry in the i th place (the place corresponding to d i ) if and only if { m d i + 1 } + { n d i + 1 } 1 .

Answers

Proof. We write the integers m , n , m + n in base d :

m = j = 0 k a j d j , n = j = 0 k b j d j , m + n = j = 0 k c j d j ,

where 0 a i < d , 0 b i < d , 0 c i < d (we can take the same k , greater than the lengths of the representations of m , n and m + n in base d ).

Reducing modulo d i + 1 , where 0 i < k , we obtain

j = 0 i a j d j + j = 0 i b j d j j = 0 i c j d j ( mod d i + 1 ) ,

so there is some integer r i + 1 such that

j = 0 i a j d j + j = 0 i b j d j = j = 0 i c j d j + r i + 1 d i + 1 . (1)

This equality defines r i + 1 . We put also r 0 = 0 . We want to compute c i , r i + 1 , knowing a i , b i . By equation (1) for two successive values of i ,

j = 0 i a j d j + j = 0 i b j d j = j = 0 i c j d j + r i + 1 d i + 1 , j = 0 i 1 a j d j + j = 0 i 1 b j d j = j = 0 i 1 c j d j + r i d i .

By subtracting these two equalities, we obtain

( a i + b i ) d i = c i d i + r i + 1 d i + 1 r i d i ,

thus

a i + b i + r i = c i + r i + 1 d .

Since 0 c i < d ,

r i + 1 a i + b i + r i d = r i + 1 + c i d < r i + 1 + 1 ,

so

r i + 1 = a i + b i + r i d ,

and r 1 = a 0 + b 0 d = a 0 + b 0 + r 0 d .

To summarize, we compute c i , r i by the following induction rules, for i [ [ 0 , k ] ] :

r 0 = 0 (2) { r i + 1 = a i + b i + r i d = ( a i + b i + r i ) d , c i = ( a i + b i + r i ) d r i + 1 = ( a i + b i + r i ) % d , (3)

where x y and x%y denote the quotient and remainder in the Euclidean division of x by y (as in computer programs). This is the usual algorithm to compute a sum in base d . This shows that r i + 1 is the carry corresponding to the sum of coefficients of d i , reported to the coefficient c i + 1 of m + n , and this carry satisfies equation (1).

We note that r i 0 . Moreover, r 0 = 0 1 . If r i 1 , then a i + b i + r i 2 d 1 , so a i + b i + r i < 2 d , thus r i + 1 = a i + b i + r i d 1 . This induction shows that r i = 0 or r i = 1 for all i .

Now we can answer to the question.

We note that

{ m d i } = m d i m d i = m d m d i d i .

Here

m = ( a k d k i + + a i ) d i + a i 1 d i 1 + + a 0 ,

where 0 a i 1 d i 1 + + a 0 < d i (see Problem 31). Therefore

m d i = a k d k i + + a i , d i m d i = a k d k + + a i d i , m d i m d i = a i 1 d i 1 + + a 0 ,

Therefore

{ m d i } = a i 1 d i 1 + + a 0 d i ,

and replacing i by i + 1 ,

{ m d i + 1 } = 1 d i + 1 j = 0 i a j d j . (4)

Similarly

{ n d i + 1 } = 1 d i + 1 j = 0 i b j d j . (5)

By equation (1),

r i + 1 0 r i + 1 = 1 j = 0 i a j d j + j = 0 i b j d j j = 0 i c j d j = d i + 1 j = 0 i a j d j + j = 0 i b j d j d i + 1

By equations (4) and (5), this implies that

{ m d i + 1 } + { n d i + 1 } 1 .

Conversely, if { m d i + 1 } + { n d i + 1 } 1 , then by the same equations (4) and (5),

j = 0 i a j d j + j = 0 i b j d j d i + 1 . (6)

Assume, for the sake of contradiction, that r i + 1 = 0 . Then equation (1) gives

j = 0 i a j d j + j = 0 i b j d j = j = 0 i c j d j ( d 1 ) ( 1 + d + + d i ) = d i + 1 1 ,

so

j = 0 i a j d j + j = 0 i b j d j < d i + 1 ,

in contradiction with (5). Therefore r i + 1 0 .

In conclusion

r i + 1 0 { m d i + 1 } + { n d i + 1 } 1 .

Note: to give an example of the algorithm given by (2) and (3), consider the sum 59237 + 73715 = 132952 . The following Python program compute this sum using (2) and (3):

d = 10
a = [7, 3, 2, 9, 5, 0]
b = [5, 1, 7, 3, 7, 0]
k = len(a)
c = [0] * len(a)

r = 0
for i in range(k):
    u = a[i] + b[i] + r
    c[i] = u % d
    r    = u // d

print(c)
         [2, 5, 9, 2, 3, 1]

User profile picture
2025-01-05 15:27
Comments