Second Summer 2003

Homework Assignment One: Five Little Puzzles
You are to

If you are not able to solve a problem:

The point of this assignment being that you are to devise:

You also need to argue that

Even if you don't know how to solve a problem write something.

Carefully taken observations or viewpoints can be given partial credit.

Puzzle One (10 points) You are given two different length strings that have the characteristic that they both take exactly one hour to burn. However, neither string burns at a constant rate. Some sections of the strings burn very fast; other sections burn very slowly. All you have to work with is a box of matches and the two strings. Describe an algorithm that uses the strings and the matches to calculate when exactly 45 minutes have elapsed.

Hint: (Easier, and helpful question) How do you measure 30 minutes?

Puzzle Two (30 points) A captive queen weighing 195 pounds, her son weighing 90 pounds, and her daughter weighing 165 pounds, were trapped in a very high tower. Outside their window was a pulley and rope with a basket fastened on each end. They managed to escape by using the baskets and a 75-pound weight they found in the tower. How did they do it? The problem is anytime the difference in weight between the two baskets is more than 15 pounds, someone might get killed. Describe an algorithm that gets them down safely.

Hint: The issue here is that one does not quite understand how the pulley works and what we mean by safety. So here's a brief random experiment to get an idea. Put the 75 pounds weight in one basket and let it drop to the ground. Stay away while the other basket is coming up fast. When it's over put the son in the other basket and lower him slowly. 90-75=15 so that's OK. When the son is on the ground tell him to step out of the basket, let go, and run away so the weight doesn't fall on his head. Now the 75 pound weight is on the ground and the empty basket is up. Son joins the 75 pound weight in the basked on the ground and the daughter (which has 165 pounds) steps in the empty basket. Now in two moves you can have first the daughter on the ground then (again as in the beginning) the son on the ground. So to get the kids out of the tower is possible, as described above. But you need to save the queen also, so work out a complete solution.

Puzzle Three (30 points) A farmer lent the mechanic next door a 40-pound weight. Unfortunately, the mechanic dropped the weight and it broke into four pieces. The good news is that, according to the mechanic, it is still possible to use the four pieces to weight any quantity between one and 40 pounds on a balance scale. How much did each of the four pieces weigh? (Note: You can weigh a 4-pound object on a balance by putting a 5-pound weight on one side and a 1-pound weight on the other).

Hint: Suppose it breaks into four pieces of 3, 5, 11, and 21 pounds. What quantities can we weigh with these four? The answer is: 3, 5, 11, 21, but also 5 - 3, 11 - 5, 11 - 3, 21 - 11, 21 - (3 + 5), 11 - (3 + 5), and so on. But we want to cover everything between 1 and 40 so we can easily tell (given a decomposition) if it's good or not. How many such decompositions are there? Can we check them all?

Puzzle Four (20 points) Write a program that reads two integer numbers (call them x and y) and prints back the larger of the two. Your program can only use the following kind of operations: +, -, division (/) and Math.abs(int) (which can calculate the absolute value of a number). This is not a difficult problem.

Hint: This problem appears among those indexed in Lab Notes Two. And the solution is there also.

Puzzle Five (10 points) Given a stricly positive integer (say, 5) you are to design an algorithm to calculate its square root. This problem is similar to the problem discussed in the ClassPak on pages 39-40. Here now are a couple of observations that should be getting you started on your solution. You are to use the following observations to the best of your abilities and come up with an algorithm that does not use Math.sqrt(int) or any similar shortcuts that Java might offer to you.

We make an initial guess x0 > 0 for Math.sqrt(5). If we guessed right already (how do we check?) then we are done. But if x0 is not the square root of 5 then x0 is either smaller or bigger than the square root we're looking for. When it's smaller we have:

x0 < Math.sqrt(5)              // [1] 
and when it's bigger we have
Math.sqrt(5) < x0              // [2]  
In both cases multiply by Math.sqrt(5) and divide by x0 to obtain equivalent expressions:
Math.sqrt(5) < 5 / x0          // [1] 
Math.sqrt(5) > 5 / x0          // [2] 
Combining that regardless of the original guess x0 we have either:
x0 < Math.sqrt(5) < 5 / x0 
5 / x0 < Math.sqrt(5) < x0 
Therefore, if we take the average of x0 and 5/x0, namely
x1 = (x0 + 5 / x0) / 2 
the resulting value must be closer to Math.sqrt(5) which is what we want to find.

Hint: Make a guess, then improve it (until it's good enough). Illustrate with an example (run a few iterations on paper, by hand, the way we did in the notes with the loan and interest problem).

Extra Puzzle: (due to Sriram). You have two vessels, a 3 gallons one, and a 5 gallons one. And you're on the river bank, plenty of water. I ask you to give me exactly 4 gallons of water from the river. Can you do that using the two vessels that you have? Explain how.

Hint: Fill the 5 gallon vessel with water. Pour as much as you can from it into the 3 gallon one. You are left with 2 gallons. So we can measure 2 gallons. If I had asked for 2 gallons (instead of 4) we would have been finished already. Work along these lines to get the problem done.

Last updated: Jun 25, 2003 by Adrian German for A201