# Ada Solution Manual

Exercises 1.1

1. Do some research on al-Khorezmi (also al-Khwarizmi), the man from whose name the word “algorithm” is derived. In particular, you should learn what the origins of the words “algorithm” and “algebra” have in common. 2. Given that the ofﬁcial purpose of the U.S. patent system is the promotion of the “useful arts,” do you think algorithms are patentable in this country? Should they be? 3. a. Write down driving directions for going from your school to your home with the precision required by an algorithm. b. Write down a recipe for cooking your favorite dish with the precision required by an algorithm. 4. Design an algorithm for swapping two 3 digit non-zero integers n, m. Besides using arithmetic operations, your algorithm should not use any temporary variable. 5. Design an algorithm for computing gcd(m, n) using Euclid’s algorithm. 6. Prove the equality gcd(m, n) = gcd(n, m mod n) for every pair of positive integers m and n. 7. What does Euclid’s algorithm do for a pair of numbers in which the ﬁrst number is smaller than the second one? What is the largest number of times this can happen during the algorithm’s execution on such an input? 8. What is the smallest and the largest number of divisions possible in the algorithm for determining a prime number? 9. a. Euclid’s algorithm, as presented in Euclid’s treatise, uses subtractions rather than integer divisions. Write a pseudocode for this version of Euclid’s algorithm. b. Euclid’s game (see [Bog]) starts with two unequal positive numbers on the board. Two players move in turn. On each move, a player has to write on the board a positive number equal to the difference of two numbers already on the board; this number must be new, i.e., different from all the numbers already on the board. The player who cannot move loses the game. Should you choose to move ﬁrst or second in this game? 10. The extended Euclid’s algorithm determines not only the greatest common divisor d of two positive integers m and n but also integers (not necessarily positive) x and y, such that mx + ny = d.

1

a. Look up a description of the extended Euclid’s algorithm (see, e.g., [KnuI], p. 13) and implement it in the language of your choice. b. Modify your program for ﬁnding integer solutions to the Diophantine equation ax + by = c with any set of integer coefﬁcients a, b, and c. 11. Locker doors There are n lockers in a hallway, numbered sequentially from 1 to n. Initially all the locker doors are closed. You make n passes by the lockers, each time starting with locker #1. On the ith pass, i = 1, 2, . . . , n, you toggle the door of every ith locker: if the door is closed, you open it; if it is open, you close it. For example, after the ﬁrst pass every door is open; on the second pass you only toggle the even-numbered lockers (#2, #4, . . .) so that after the second pass the even doors are closed and the odd ones are open; the third time through, you close the door of locker #3 (opened from the ﬁrst pass), open the door of locker #6 (closed from the second pass), and so on. After the last pass, which locker doors are open and which are closed? How many of them are open?

2

Hints to Selected Exercises 1.1

1. It is probably faster to do this by searching the Web, but your library should be able to help, too. 2. One can ﬁnd arguments supporting either view. There is a well-established principle pertinent to the matter, though: scientiﬁc facts or mathematical expressions of them are not patentable. (Why do you think this is the case?) But should this preclude granting patents for all algorithms? 3. You may assume that you are writing your algorithms for a human...

Please join StudyMode to read the full document