Homework 0 - Base conversion and program design
Notes on hexadecimal
Some of you unfamiliar with hexadecimal notation may be wondering how to represent digits about 9. The answer is you use letters: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
For example, the number 3162 in hexadecimal is C5Ah, since 3162 = 12 × 162 + 5 × 162 + 10 × 160. The convention is that we put a lowercase 'h' after the number to let the reader know the number is in hexadecimal in case no letters appear. Also, for other bases, it is customary to subscript a number with the base when base 10 is not used, e.g. 5227 = 261.
Notes on number conversion
Realize that the digit representation (sequence of digits) of a number in a given base is the coefficients of the powers of base that need to be added together. For example in ordinary base 10,
An alternate, and perhaps more useful way of interpreting a digit representation (in the context of this homework), is to think of the sequence as a set of remainders. Given a number N and its digit representation anan-1an-2…a1a0 of N in base B,
Suppose we now divide through by a power of B,
So from the above, we see that the lowest digit was extracted: a0 = Remainder(N ÷ B). If we repeat this process with the integer part that we got, we would then extract the next digit, and so on.
Therefore, we develop a general algorithm for finding the digits of a number:
- Let N be the number for which we want to find the digit representation in base B.
- Divide N by B, write down the quotient (integer part) and remainder.
- Set N equal to the integer quotient found above and go to step 2 unless the new N is zero, then go to step 4
- The remainders written down are the reverse of the digit representation
For a concrete example, see "Change of radix," under this Wikipedia article.
Notes on program design
Glen should have covered how to effectively describe a program or algorithm in class. You should also already have an example (FACTOR) to give you an idea of what we're looking for. The idea is to describe the method by which you will accomplish a task, in enough detail that anybody given your description can perform the calculations. Of course, you must describe the method using basic operations; the factor algorithm uses only basic arithmetic operations, storage operations, comparison operations, etc.
- Bad: Find all the numbers that divide N. The factors of N are …
- Not quite so bad: For each number I less than N, find if I divides N. If it does then …
- Good: For each number I less than N, compute R = N mod I. If R = 0, then …
- Best: See Glen's example.