You are on page 1of 7

Dr. Z.

s Number Theory Lecture 4 Handout: Representation of integers; Addition and Multiplication


By Doron Zeilberger
In Lecture 1 we met the unary representation, aka as the cavemans way for representing positive
integers. It is an eective way, i.e. one can do (at least in principle) arithmetics: add, multiply,
divide etc. But it is inecient. To represent one googol, we would need more that googol bytes
in hour computer memory.
Luckily we have the positional system. The traditional base is base 10, and we are so used to it
that we forget that when we write the number 30201 it but shorthand for
3 10
4
+ 0 10
3
+ 2 10
2
+ 0 10
1
+ 1 10
0
,
or, more succinctly,
3 10
4
+ 2 10
2
+ 1 10
0
.
In other words, 1, 10, 100, 100, . . . are the building blocks that can be used to form any integer,
with coecients from 1 to 9 (using the sparse notation, or 0 to 9, using the dense notation.
But it is a uke that we, humans, have ten ngers. A more natural way is to use the binary
system, and use as building blocks the powers of two: 1, 2, 4, 8, 16, . . ..
Important Algorithm: How to convert to binary (in sparse notation).
Input: A positive integer n (given in unary, decimal, or any other way)
Output: B(n) expressing n as a sum of powers of 2
Method: If n = 0 then return 0 (or nothing). Otherwise, nd the largest k such that 2
k
< n,
then
B(n) = 2
k
+ B(n 2
k
) .
Remark: This is an example of a recursive algorithm.
Problem 4.1: Convert one thousand and one to binary (in sparse notation).
Looking at the powers of 2, in turn, 1, 2
1
= 2, 2
2
= 4, 2
3
= 8, 2
4
= 16, 2
5
= 32, 2
6
= 64, 2
7
=
128, 2
8
= 256, 2
9
= 512, 2
10
= 1024, . . ., we see that 1024 exceeds 1001, so the highest power of 2
that is 1001 is 2
9
= 512. So
B(1001) = 2
9
+ B(1001 512) = 2
9
+ B(489) .
Looking at the powers of 2, in turn, 1, 2
1
= 2, 2
2
= 4, 2
3
= 8, 2
4
= 16, 2
5
= 32, 2
6
= 64, 2
7
=
128, 2
8
= 256 . . ., we see that the highest power of 2 that is 489 is 2
8
= 256. So
B(489) = 2
8
+ B(489 256) = 2
8
+ B(233) .
1
Looking at the powers of 2, in turn, 1, 2
1
= 2, 2
2
= 4, 2
3
= 8, 2
4
= 16, 2
5
= 32, 2
6
= 64, 2
7
= 128 . . .,
we see that the highest power of 2 that is 233 is 2
7
= 128. So
B(233) = 2
7
+ B(233 128) = 2
7
+ B(105) .
Looking at the powers of 2, in turn, 1, 2
1
= 2, 2
2
= 4, 2
3
= 8, 2
4
= 16, 2
5
= 32, 2
6
= 64 . . ., we see
that the highest power of 2 that is 105 is 2
6
= 64. So
B(105) = 2
6
+ B(105 64) = 2
6
+ B(41) .
Looking at the powers of 2, in turn, 1, 2
1
= 2, 2
2
= 4, 2
3
= 8, 2
4
= 16, 2
5
= 32 = 64 . . ., we see that
the highest power of 2 that is 41 is 2
5
= 32. So
B(41) = 2
5
+ B(41 32) = 2
6
+ B(9) .
Looking at the powers of 2, in turn, 1, 2
1
= 2, 2
2
= 4, 2
3
= 8, . . ., we see that the highest power of
2 that is 9 is 2
3
= 8. So
B(9) = 2
3
+ B(9 8) = 2
3
+ B(1) .
Looking at the powers of 2, in turn, 2
0
= 1, 2
1
= 2, . . ., we see that the highest power of 2 that is
1, is 2
0
= 1. So
B(1) = 2
0
.
Now we do the return journey
B(9) = 2
3
+ B(1) = 2
3
+ 2
0
.
B(41) = 2
5
+ B(9) = 2
5
+ 2
3
+ 2
0
.
B(105) = 2
6
+ 2
5
+ 2
3
+ 2
0
.
B(233) = 2
7
+ B(105) = 2
7
+ 2
6
+ 2
5
+ 2
3
+ 2
0
.
B(489) = 2
8
+ 2
7
+ 2
6
+ 2
5
+ 2
3
+ 2
0
.
2
B(1001) = 2
9
+ B(489) = 2
9
+ 2
8
+ 2
7
+ 2
6
+ 2
5
+ 2
3
+ 2
0
.
Ans. to 4.1: The sparse binary representation of one thousand-and-one is
2
9
+ 2
8
+ 2
7
+ 2
6
+ 2
5
+ 2
3
+ 2
0
.
Problem 4.1: Write one thousand and one in dense binary representation.
Solution to 4.1: We rst do as in 4.1, but include the powers of 2 that did not show up, putting
a 0 in front, and putting a 1 in front of those that did show up.
1001 = 1 2
9
+ 1 2
8
+ 1 2
7
+ 1 2
6
+ 1 2
5
+ 0 2
4
+ 1 2
3
+ 0 2
2
+ 0 2
1
+ 1 2
0
.
Problem 4.1: Write one thousand and one in binary (base 2).
Solution to 4.1: We rst do as in 4.1, but then only list the coecients without writing the
powers of 2. This is shorthand.
So we get 1111101001.
General Base b
Every integer n can be written as
n =
k

i=0
a
i
b
i
, 0 a
i
< b .
Important Algorithm: How to convert to general base b (in sparse notation).
Input: A positive integer n (given in unary, decimal, or any other way)
Output: B(n) expressing n as a sum of powers of b times integers between 0 and b 1
Method: If n = 0 then return 0 (or nothing). Otherwise, nd the largest k such that b
k
< n,
then let a
k
be the quotient of dividing n by b
k
,
B(n) = a
k
b
k
+ B(n a
k
b
k
) .
Problem 4.2: Convert one hundred and fteen to base 5.
Solution of 4.2: The powers of 5 are 5
0
= 1, 5
1
= 5, 5
2
= 25, 5
3
= 125. The largest one that is
smaller than 115 is 5
2
. Dividing 115 by 25 we get a
2
= 4, and the remainder is 15
B(115) = 4 5
2
+ B(15) .
3
The largest power of 5 that is smaller than 15 is 5
1
. Dividing 15 by 5 we get a
1
= 3, and the
remainder is 0. So
B(15) = 3 5
1
.
Going back
B(115) = 4 5
2
+ 3 5
1
.
Putting all the powers of 5 (including those that do not show up):
115(base 10) = 4 5
2
+ 3 5
1
+ 0 5
0
.
Finally, just looking at the coecients, we get
115(base 10) = 430(base 5) .
How to add-up two integers in base b
Input: Two integers m and n written as polynomials in b, i.e. in base b, using the sparse notation.
Output: Their sum, m + n also written the same way.
Step 1: Once and for all create the addition table, for any integers 0 i < j < b, nd integers
0 c
i,j
1, 0 d
i,j
< b, such that
i + j = c
i,j
b + d
i,j
Step 2: Add m and n viewing them as polynomials in the variable b.
Step 3: Use the addition-table from Step 1, to gure out the coecients, collect terms, repeat if
necessary.
Problem 4.3: Find, in base 3,
(2 3
5
+ 2 3 + 1 3
0
) + (2 3
5
+ 2 3
2
+ 1 3 + 1 3
0
)
Solution to 4.3:
Step 1: 0 + 1 = 1, 0 + 2 = 2, 1 + 2 = 1 3 + 0, 2 + 2 = 1 3 + 1
Step 2:
(2 3
5
+2 3 +1 3
0
) +(2 3
5
+2 3
2
+1 3 +1 3
0
) = (2 +2) 3
5
+2 3
2
+(2 +1) 3 +(1 +1) 3
0
.
Step 3.:
(13+11)3
5
+23
2
+(13)3+(2)3
0
= 13
6
+13
5
+23
2
+13
2
+23
0
= 13
6
+13
5
+(2+1)3
2
+23
0
4
= 1 3
6
+ 1 3
5
+ (3) 3
2
+ 2 3
0
= 1 3
6
+ 1 3
5
+ 1 3
3
+ 2 3
0
.
Ans. to 4.3: 1 3
6
+ 1 3
5
+ 1 3
3
+ 2 3
0
.
How to multiply two integers in base b
Input: Two integers m and n written as polynomials in b, i.e. in base b, using the sparse notation.
Output: Their product, mn also written the same way.
Step 1: Once and for all create the multiplication table, for any integers 0 i < j < b, nd integers
0 c
i,j
< b, 0 d
i,j
< b, such that
i j = c
i,j
b + d
i,j
Step 2: Multiply m and n viewing them as polynomials in the variable b.
Step 3: Use the multiplication table from Step 1, to gure out the coecients, collect terms, repeat
if necessary.
Problem 4.4: Find, in base 3,
(2 3
5
+ 1 3
0
) (2 3
5
+ 2 3
0
)
Solution to 4.4:
Step 1:
1 2 = 2 , 2 2 = 1 3 + 1 .
Step 2:
(23
5
+13
0
)(23
5
+23
0
) = (22)3
10
+(22)3
5
+(12)3
5
+(12)3
0
= (13
1
+13
0
)3
10
+(13
1
+13
0
)3
5
+23
5
+23
0
= 13
11
+13
10
+13
6
+13
5
+23
5
+23
0
= 13
11
+13
10
+13
6
+(1+2)3
5
+23
0
= 13
11
+13
10
+13
6
+(3)3
5
+23
0
= 13
11
+13
10
+13
6
+(3
1
)3
5
+23
0
= 13
11
+13
10
+13
6
+13
6
+23
0
= 13
11
+13
10
+23
6
+23
0
.
Ans. to 4.4: 1 3
11
+ 1 3
10
+ 2 3
6
+ 2 3
0
.
Note: For the sake of convenience, we express that exponents in the usual, decimal notation, but
of course, in a perfect world, these should also be written in base 3 (or whatever base you are
using).
A Recursive Algorithm for Multiplication
For the sake of clarity, we will use base 10, and the usual way of representing integers.
5
Input: Two integers with k digits, m and n.
Step 1: If k is even write them as
m = a 10
k/2
+ b , n = c 10
k/2
+ d , ,
where a, b, c, d are integers with k/2 digits.
If k is odd, view them as integers with k + 1 digits, and do as above.
Step 2: Compute, recursively, the four products
A = a c , B = a d , C = b c , D = b d .
Step 3: Write
m n = A 10
k
+ (B + C)10
k/2
+ D 10
0
,
distribute and simplify, if necessary.
A Better Recursive Algorithm for Multiplication: Karatsuba algorithm
Input: Two integers with k digits, m and n.
Step 1: If k is even write them as
m = a 10
k/2
+ b , n = c 10
k/2
+ d , ,
where a, b, c, d are integers with k/2 digits. If k is odd, view them as integers with k +1 digits,
and do as above.
Step 2: Compute, recursively, the three products
A = a c , C = (a + b) (c + d) , D = b d .
Step 3: Using addition-subtraction, nd
C

= C AD
Step 4: Compute
m n = A 10
k
+ C

10
k/2
+ D 10
0
,
distribute and simplify, if necessary.
Problem 4.5: Using Karatsubas algorithm (no credit for other methods!), to nd the product
35 56 .
6
Step 1:
35 = 3 10 + 5 , 56 = 5 10 + 6 ,
so a = 3, b = 5, c = 5, d = 6.
Step 2:
A = 3 5 = 15, C = (3 + 5) (5 + 6) = 88 , D = 5 6 = 30 .
Step 3:
C

= C AD = 88 (15 + 30) = 88 45 = 43 .
Step 4:
35 56 = (15) 10
2
+ (43) 10 + 30 = 1500 + 430 + 30 = 1960 .
Ans. to 4.5: 1960 .
7

You might also like