This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

John has a small and a large hourglasses. The small one can measure 5 minutes and the large one can measure 7 minutes. How can he measure 16 minutes with 2 hourglasses running together?

The rule is when any one of the hourglasses finishes leaking, that hourglass must be flipped over immediately to keep it running.

Start off by starting both the 7 and the five minute timers. Flip each one as it runs out. End up by flipping the 7 minute timer 1 once, and the 5 minute twice. This generates 14 minutes total on the 7 minute timer, and 15 minutes on the five minute timer.

Once the second run of the 7 minute timer has elapsed (14 minutes into the process), start your stopwatch. The 3rd run of the 5 minute timer has 1 minute remaining at this point. Once this minute runs through, simply run the 5 minute timer three more times.

Start the timing by turning over both hourglasses. Once the 5 minute glass empties, turn it over again. Once the 7 minute glass empties, turn over the 5 minute glass (It will now run for 2 minutes). Once it empties, turn over the 7 minute glass. You have now run for 16 minutes.

George, you missed the rule. as soon as a glass empties, you must flip it. You can't hold the 7-min glass for the two minutes it takes the 5 to run down.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Originally posted by Brett Fitzgerald: Start off by starting both the 7 and the five minute timers. Flip each one as it runs out. End up by flipping the 7 minute timer 1 once, and the 5 minute twice. This generates 14 minutes total on the 7 minute timer, and 15 minutes on the five minute timer.

Once the second run of the 7 minute timer has elapsed (14 minutes into the process), start your stopwatch. The 3rd run of the 5 minute timer has 1 minute remaining at this point. Once this minute runs through, simply run the 5 minute timer three more times.

Brett

While I think George has a better solution (only five moves total, including starting both timers at t=0), Brett's can be modified so that it doesn't need fifteen minutes of prep work:

Start off by starting both the 7 and the 5 minute timers. Flip each one as it runs out. End up by flipping the 7 minute timer twice, and the 5 minute twice. When the five minute timer runs out the third time (after fifteen minutes), there is one minute's worth of sand in the bottom of the seven minute timer. Just turn it over. When it runs out, the 16 minutes are up. (Seven moves)

3*5 + (3*5 - 2*7) = 16

Assumption: The rate of sand falling through the glasses is constant. i.e. If you run the seven minute timer for one minute and then flip it over, it will take one minute to run out. [ March 28, 2006: Message edited by: Ryan McGuire ]

Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1041

4

posted

0

Originally posted by fred rosenberger: George, you missed the rule. as soon as a glass empties, you must flip it. You can't hold the 7-min glass for the two minutes it takes the 5 to run down.

Ok, between t=7 and t=9, just keep flipping the seven mitute timer over and over really fast so that the sand stays in one end. [ March 28, 2006: Message edited by: Ryan McGuire ]

Of course if you do that, the solution is no longer "best" in terms of minimizing the number of flips, is it?

That rule seemed pretty silly and arbitrary to me anyway. There would be no real-world reason you couldn't just set the timer on its side. Regardless though, to me it seems more useful to minimize startup time than to minimize the number of flips. For that reason, I like Ryan's last version best.

The rule is when any one of the hourglasses finishes leaking, that hourglass must be flipped over immediately to keep it running.

i think the above thing needs to be taken care off...

so here is a solution

start with both the glasses

after the small hour glass finishes turn it. Time at that point will be 5 min.

after the big hour glass finishes turn it. Time at that point will be 7 min.

after small hour glass finishes turn both Big glass will have 3 min remaining and small 5 min and the time will be 10 min.

after the big hour glass finishes turn both. in turn both big will have 7 min and small will have 3 min and the time will be 13 min.

when the small gets emptied it will be 16 min.

Karthikeyan<br />SCJP 1.4, SCWCD.

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

posted

0

[Karthikeyan]: i think the above thing needs to be taken care off...

No, I think it's already covered in all solutions except George's. Though for example Stefan didn't actually say that you continue flipping the small container as necessary, even after you're no longer paying attention to it. But this is an easy modification to his solution.

I didn't see a rule to minimize something, but in real world I would prefer to minimize total elapsed time.

And in reality I would have missed flipping the small glass around (and if not, I would refuse that and break it at the wall - silly rule - and get disqualified - that's the sad truth).

I like Karthikeyans most, because his solution solves the problem in 16 min, while my first has a 5 minutes startup-phase (like a jit-compiler). I guess my solution would be less error-prone, because you can forget about the small glass soon (just turn it when empty) and after an initial not-empty-turn, you only have two empty-turns on the big glass. K's solution needs more concentration - so in vital environments I would prefer mine

At the 5 minute point, the small hourglass will run out. Flip it. There should be 2 minutes left on the large hourglass.

At the 7 minute point, the large hourglass will run out. Flip it. There should be 3 minutes left on the small hourglass.

At the 10 minute point, the small hourglass will run out. Flip it. There should be 4 minutes left on the large hourglass.

At the 14 minute point, the large hourglass will run out. Flip it. There should be 1 minute left on the small hourglass.

At the 15 minute point, the small hourglass will run out. Flip *both* hourglasses. Since the large hourglass has ran for 1 minute from its last flip, there should be 1 minute left on the large hourglass.

At the 16 minute point, the large hourglass will run out.

Henry's solution appears identical to the soltution suggested by Ryan in his first post this thread. Which also appears to have been overlooked by several subsequent posters. So I suppose it's good to explicitly state all the steps in hopes that people will actually see the darn thing. It's pretty straightforward, and does not waste any time in setup steps. It measures out sixteen seconds, and it takes sixteen seconds to do so. What else do we need?

Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1041

4

posted

0

Originally posted by Jim Yingst: What else do we need?

Some way to get around the assumption in my original post, which, I've found out through experimentation, is NOT true in real life. If you start an hour glass and let it run for one minute and then flip it back over, it takes more than one minute to run out.