aspose file tools*
The moose likes Programming Diversions and the fly likes How to measure 16 minutes using 2 hourglasses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "How to measure 16 minutes using 2 hourglasses" Watch "How to measure 16 minutes using 2 hourglasses" New topic
Author

How to measure 16 minutes using 2 hourglasses

Rakesh Joshi
Ranch Hand

Joined: Feb 24, 2006
Posts: 218
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.


Life is a Game play it.
Brett Fitzgerald
Greenhorn

Joined: Mar 21, 2006
Posts: 6
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
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Flip the small and the big.
After 5 min. the small is empty, and 2 mins left in the big.

Start measuring with this left 2 mins, and make two more complete turns of the big (2 + 2 * 7 = 2 + 14 = 16).


http://home.arcor.de/hirnstrom/bewerbung
George Harris
Ranch Hand

Joined: May 05, 2003
Posts: 84
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.

7 + 2 + 7.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
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: 1013
    
    3
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 ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
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
[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.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
Well this is easy

Turn both the 5min and 7min and take out a stopwatch from you pocket
play with these for 16 mins and there you go!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19062
    
  40

Start both hourglasses running.

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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: 1013
    
    3
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.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

it takes more than one minute to run out.


Does that include the time to flip the hourglass.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to measure 16 minutes using 2 hourglasses