Vous êtes sur la page 1sur 2

Theory and Practice of Programming Theory Problem Sheet 1 Work through the problems below on pen and paper.

If you nish all the other questions, have a go at the algorithmic brain teaser. The implementation challenge requires you to write some code in your own time to compute the answer. 1. Assume I have a physical copy of the Manhattan telephone directory which has 1,048,576 entries. I would like to look up someones phone number by their surname. What is the optimal strategy for nding the number? How many entries do I need to look at in the best and worst case? 2. We now go all the way back to 300BC and Euclids algorithm for nding the greatest common devisor of two integers. Input: two positive integers, a and b where a is the larger of the two Algorithm pseudocode: (i) Divide a by b and store the remainder in r (ii) If r equals 0 then stop: the greatest common devisor is b (iii) Otherwise, copy b into a and r into b and return to step (i) Trace the values of each variable through each step of the algorithm when called with the following values: (a) a = 3, b = 1 (b) a = 36, b = 15 (c) a = 1071, b = 462 3. In the algorithm above, we assumed we had a means to perform division and compute remainders. Imagine implementing Euclids algorithm on a computer which only supports the mathematical operations of addition and subtraction. Describe an algorithm for computing the quotient, q, and remainder, r, when dividing an integer a by an integer b. In other words, which solves for q and r in: a = qb + r. 4. Trace the values of each variable through each step of your division algorithm when called with the same pairs of values as in question 4. 5. The Fibonacci sequence is dened as follows: the nth Fibonacci number is given by the sum of the (n 1)th and (n 2)th Fibonacci numbers, the rst two Fibonacci numbers are 1 and 1. (a) Write pseudocode for a recursive function which takes n as input and returns the nth Fibonacci number. Hint: you should rst check for base cases. If it isnt a base case, restate the problem in simpler terms and make recursive calls before nally returning the answer. (b) Write pseudocode for an iterative function which takes n as input and returns the nth Fibonacci number. You may not use recursion. Hint: work your way up from i = 0 to i = n, keeping track of the relevant values as you iterate. 6. Algorithmic brain teaser: You are given a stable of 25 horses. You must determine the fastest, second fastest and third fastest horse. At most, 5 horses can race together at a time and you have no timing device (i.e. you can only observe the nishing positions of each race). Find the minimum number of races in which this can be done.

7. Implementation challenge: Implement the algorithm you designed in question 3 and use it to nd the greatest common devisors of 123,456,789 and 987,654,321. Check you get the same answer when you implement division using the algorithm designed in question 3. How much slower is your division function than the standard Java division operator? 8. Implementation challenge: Implement the algorithm you designed in question 5b and use it to nd the 39th Fibonacci number. Now implement the algorithm you designed in question 5a. Can it nd the 39th Fibonacci number in a feasible amount of time? How much slower than the iterative version is it?

Vous aimerez peut-être aussi