function f = crt ( m, r )

%*****************************************************************************80
%
%% crt() reconstructs a value using the Chinese remainder theorem.
%
%  Discussion:
%
%    The value F that is returned will have the property that
%      F mod M(i) = R(i) for all i.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 December 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M(n), the bases or moduluses.
%    The moduluses must be coprime.
%
%    integer R(n), the remainders.
%
%  Output:
%
%    integer F, the reconstructed integer.
%
  s = prod ( m ) ./ m;

  [ g, t, u ] = gcd ( s, m );
%
%  t(i) satisfies: s * t(i) + m * u(i) = g(i);
%  The values of g and u are not actually needed here.
%
  f = s .* t * r';

  return
end


