function subset_sum_test ( )

  m = 30;
  n = 8;
  a = [ 2, 3, 9, 14, 23, 11, 2, 5 ];
  t = subset_sum ( m, n, a );

  if ( t(m) == 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, '  Could not find a sum equal to %d\n', m );
  else
    fprintf ( 1, '  %d = ', m )
    i = m;
    while ( 0 < i )
      s = t(i);
      if ( i < m ) 
        fprintf ( 1, ' + ' );
      end
      fprintf ( 1, '%d', s )
      i = i - s;
    end
    fprintf ( 1, '\n' );
  end

  return
end
function t = subset_sum ( m, n, a )

  t = zeros ( m, 1 );

  for i = 1 : n
    for j = m - a(i) : -1 : 0

      if ( j == 0 )
        if ( t(a(i)) == 0 )
          t(a(i)) = a(i)
        end
      elseif ( t(j) ~= 0 && t(j+a(i)) == 0 )
        t(j+a(i)) = a(i)
      end

    end

  end
  
  return
end
