Professional Documents
Culture Documents
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