|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
In the kingdom of Wunhundredtwentia, the jail system is extensive, with 200 cells all in a row. Each cell's door has a key, which when turned toggles the door's lock. That is, if the door was unlocked, then turning the key locks it; if locked, then turning unlocks it.
The Royal Jailer has a curious habit: Every evening, he turns the key in every single door -- numbers 1 through 200. Then, he goes back and turns the key in every other door -- numbers 2, 4, 6, ..., 200. Then, he goes back and turns the key in every third door -- numbers 3, 6, 9, ..., 198. Then, he turns the key in every fourth door -- numbers 4, 8, 12, ..., 200. And so on, until he finishes by turning the key in every 200th door (which is just one door: cell #200). After that, he falls asleep, and anybody in an unlocked cell can escape during the night. (At the start of the evening, all cells are initially locked.)
You and a baker's dozen of your friends have been framed, and falsely convicted of Meandering with Intent to Jay-walk, and are scheduled to be executed at dawn. Fortunately, each of you can name which cell to be locked away in.
Which cells do you choose?
Hints:
Extra credit, 2-5pts:
Once your program tells you the answer,
augment the answer with an explanation.
Not just what the pattern is, but why that pattern occurs.
(This is a nice illustration of the difference between
knowing an answer,
and understanding the answer.)
No points if the explanation isn't written clearly
(you may want to run it by a roommate).
1 If you need to turn in hw14 on Mon afternoon, I'll accept it until then, since I had the wrong date. Those who finished it by Friday have earned extra time for studying! ↩
While this skip-a-location approach isn't usually needed for arrays, the numeric properties of the jailer's walk make it convenient in this case.
To think about: What is the extra space-overhead incurred by this approach? Is it significant?
Answer: Well, it's an inefficiency of about 1/200, or 0.5%. This is is negligible, and reducing your memory usage by 0.5% isn't worth sacrificing how your code reflects your thinking. (Besides, trying to save that one location of memory will probably add more to your code-length than you save in data-length.)
↩home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Dec.14 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |