Crossing Hands

February 25, 2014

The hands of an analog clock occasionally cross as they revolve around the dial.

Your task is to write a progam that determines how many times the hands cross in one twelve-hour period, and compute a list of those times. When you are finisehd, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

8 Responses to “Crossing Hands”

  1. Juan Ignacio Saba said

    Here’s a piece of code that does this.

    #!/usr/bin/perl -w
    
    use strict;
    use warnings;
    
    my $hour = 0;
    my $minute = 0;
    my $second = 0;
    
    while($hour <= 11 && $minute <= 59 && $second <= 59) {
        my $h_pointer = ($hour*3600+$minute*60+$second);
        my $m_pointer = int(43200*($minute*60+$second)/3600);
    
        if($m_pointer >= $h_pointer) {
            print "$hour hours $minute minutes $second seconds\n";
            $hour += (1+($minute=0)+($second=0));
            next;
        }
    
        $second++;
        $minute += (1+($second=0)) if $second == 60;
        $hour += (1+($minute=0)) if $minute == 60;
    }
    

    Here’s the output:

    0 hours 0 minutes 0 seconds
    1 hours 5 minutes 28 seconds
    2 hours 10 minutes 55 seconds
    3 hours 16 minutes 22 seconds
    4 hours 21 minutes 50 seconds
    5 hours 27 minutes 17 seconds
    6 hours 32 minutes 44 seconds
    7 hours 38 minutes 11 seconds
    8 hours 43 minutes 39 seconds
    9 hours 49 minutes 6 seconds
    10 hours 54 minutes 33 seconds
    

    The idea here was to divide the clock into 43200 positions (the number of seconds in 12 hours), then calculate at each time the position where the hour and minute hands are and finally being able to detect when the minute hand comes ahead of the hour hand.

    Note that the two hands are hardly ever at the same position, so what I tried to do was to detect when the minute hand passes from being behind the hour hand to being ahead of it.

    As an example, in the output shown above, at 01:05:27 the minute hand was slightly behind the hour hand while at 01:05:28 it was slightly ahead.

    Cheers!

  2. Problem with your problem and solution. Many analogue clocks jump the hands by the minute not by the second, even if they have a second hand. So the solutions include valid answers that only have movements by the minute. This gives and interesting result at 9:48 and 9:49, depending on the mechanism used for moving the clock hands. It is possible for the hands to cross at both times.

  3. sabapcblog said

    Hi Hill, would you elaborate on the 9:48 case? I don’t understand what you mean by the mechanisms used for moving the clock hands.

  4. On the basis of mechanically moving by the minute, during the ninth hour, the hands are at the following positions

    Time: 9:00 9:12 and later 9:24 and later 9:36 and later
    Hour hand: Major mark 9 Minor mark 46 Minor mark 47 Minor Mark 48
    Minute hand: Major mark 12 and .. minor mark 12 and … Minor mark 24 and … Minor mark 36 and …

    Now when the minute hand reaches minor mark 48, the hour hand is on 48 for a fraction of second before travelling to minor mark 49, on which it will rest until hour hand moves to major mark 10 and minute hand is on Major mark twelve. Hence there is a crossover of the hands at this point in time, however briefly.

    Depending on the way the mechanism works, there is a small overlap due to (generally) the minute hand operation preceding the hour hand operation. The internals of most mechanical “analogue” clocks are relatively cheap so the easy way is taken out and movement of hour/minute hands is based on minutes not seconds as this would require greater accuracy and more complexity in the internal workings of the clock. This would increase the expense of the construction of the clock mechanism.

    The best way to see this is to actually watch the movements of the hands as the second hand moves around the clock face.

    The problem describes above is fine when modelled ideally, that is everything is purely analog and there are no steps within the mechanism. But when looking at the real world, models do have their limits. An example that has just come to mind is the mobius strip. Any real world example, where the thickness is very much less than the width actually has two faces, the main face and the edge face. To get an real world example that only has one face requires the width of the strip to be equal to the thickness of the strip and requires an extra quarter turn to get the single face. Try showing smart children an example and someone will ask about the edge, I know, I did.

    If you see a problem in my description above, please feel free to point out what it is. After all, I am only human.

  5. In the previous reply, the additional spaces were removed so add additional spaces after the words “and later” and after “and …” to make sense of the three lines

  6. JP said

    Here’s some Racket:

    (define f (curry ~a #:min-width 2 #:align ‘right #:pad-string “0”))
    (for* ([h (in-range 12)]
    [m (in-range 60)]
    #:when (= m (+ (* h 5) (quotient m 12))))
    (printf “~a:~a\n” (f h) (f m)))

    Technically, it returns 11:59 as well, but for 30 seconds of code (most of which was looking up the particulars of ~a), that’s not half bad.

  7. Ok Hill, I understand what you’re saying and it is pretty interesting, actually, from a mechanical point of view.

    I do not think however that it was the purpose of the question to go mechanical about it, but I guess that’s for the poster to tell.

  8. By the way, not sure how my name shows up for everybody but, just to clarify, “sabapcblog” and “Juan Ignacio Saba” are the same person (me). It’s just that I changed my display name at some point but the older posts are not dynamic with regards to that (too bad).

Leave a comment