function value = luhn_checksum ( s )

%*****************************************************************************80
%
%% luhn_checksum() determines the Luhn checksum of a string.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 September 2015
%
%  Reference:
%
%    https://en.wikipedia.org/wiki/Luhn_algorithm
%
%  Input:
%
%    string S, the string of digits to be checked.
%
%  Output:
%
%    integer VALUE, the Luhn checksum for this string.
%

%
%  Count the digits in S.
%
  n = s_digits_count ( s );
%
%  Extract the digits from S.
%
  dvec = s_to_digits ( s, n );
%
%  Starting with the N-th digit, and going down by 2's, 
%  add the digit to the sum.
%
  value = sum ( dvec(n:-2:1) );
%
%  Starting with the (N-1)-th digit, and going down by 2's, 
%  double the digit, and ADD THE DIGITS to the sum.
%
  for i = n - 1 : -2 : 1
    d2 = 2 * dvec(i);
    value = value + floor ( d2 / 10 ) + mod ( d2, 10 );
  end

  value = mod ( value, 10 );

  return
end
