CSC 344
Homework 1

Assigned 22 Jan, due 5 Feb

Problems from chapter 1 of the textbook
Computing square roots:

The square root of a number tends to be either quite simple (e.g. sqrt(9) = 3) or irrational, a non-repeating infinitely long decimal. So most algorithms for computing square roots involve successive approximations: they start with a guess at the right answer, then use it to compute a closer guess, then a still closer guess, etc. until the approximation is "close enough" for the purpose at hand. (The "close enough" criterion usually determines how long the algorithm takes, acting like a size parameter.)

One simple algorithm for computing square roots is binary search. Given a positive number c, you know that the square root is somewhere between 0 and c (technically this is only true if c>1; assume that for now and we'll come back to it later). So pick a guess in the middle of that range, c/2. If (c/2)2 is larger than c, that means your guess was too high, and the actual answer is between 0 and c/2; if smaller, your guess was too low and the actual answer is between c/2 and c. In either case, you now have a range of possible answers only half as large as you had before. Repeat the process until the range is "small enough": for example, if you want an answer correct to two decimal places, repeat until the range is less than 0.01.

Another algorithm, developed by the ancient Babylonians at least 3500 years ago, is as follows. Suppose you want a square of area c. Start with a rectangle of dimensions 1 x c; this has the right area, but it's not square. So take the average of these two dimensions, yielding (1+c)/2, and use this as one dimension to get a "more nearly square" rectangle. If one dimension is (1+c)/2 and the area is c, then the other dimension must be c/((1+c)/2), or 2c/(1+c). This now has the right area, but it's not quite square either, so take the average of these two dimensions.... If you want an answer correct to two decimal places, repeat until your two dimensions are within .01 of one another.

Trace the action of each of these algorithms to find the square root of 5, correct to three decimal places. Then do the same to find the square root of 29, correct to three decimal places. (You may want to write a little program to help with this, but it's not required.)

For each of the two algorithms and each of the two numbers (5 and 29), how many iterations did it take to reach an accuracy of .1? An accuracy of .01? An accuracy of .001?

Base conversion:

Write an algorithm in pseudocode which takes in two natural numbers x and b, and produces the sequence of digits in the base-b representation of x. You may assume that b is at least 2 and at most 10. Your algorithm may use the standard arithmetic operations -- addition, subtraction, multiplication, division, and remainder -- and a table containing the characters '0', '1', '2', ... '9', but you may not assume any built-in conversion routines.
Trace the action of your algorithm as it computes the base-7 representation of 103 and the base-9 representation of 5280.

Implement, test, and debug your base-conversion algorithm in your favorite programming language -- C, C++, Java, Scheme, Prolog, perl, Pascal, etc.


Last modified: Fri Jan 23 17:37:19 EST 2004
Stephen Bloch / sbloch@adelphi.edu