function clock3 ( )

%% CLOCK3 tries to solve the clock problem by repeatedly splitting the interval.

  m1 = 120;
  m2 = 180;

  for i = 1 : 20
    [ m1, f1, m2, f2 ] = clock_interval ( m1, m2 );
    fprintf ( 1, '  %g  %g  %g  %g\n', m1, f1, m2, f2 );
  end

  print_time ( m1 );
  print_time ( m2 );

  return
end

function print_time ( m )

%% PRINT_TIME takes minutes since 12:00 and prints H/M/S.

  sec = 60 * mod ( m, 1.0 );

  min = floor ( m );
  
  hr = floor ( min / 60 );
  min = min - 60 * hr;

  fprintf ( 1, '  %dh  %dm  %gs\n', hr, min, sec );

  return
end

function [ m1, f1, m2, f2 ] = clock_interval ( m1, m2 )

%% CLOCK_INTERVAL chooses the half interval that changes the function.

  f1 = mh_diff ( m1 );
  f2 = mh_diff ( m2 );

  mm = ( m1 + m2 ) / 2.0;
  fm = mh_diff ( mm );

  if ( sign ( f1 ) == sign ( fm ) ) 
    m1 = mm;
    f1 = fm;
  else
    m2 = mm;
    f2 = fm;
  end

  return
end

function value = mh_diff ( m )

  h = m / 60.0;
  ma = mod ( m / 60.0, 1.0 );
  ha = mod ( h / 12.0, 1.0 );

  value = ha - ma;

  return
end

