function [ n_data, m, n, d ] = delannoy_values ( n_data )

%*****************************************************************************80
%
%% delannoy_values() returns some values of the Delannoy numbers.
%
%  Discussion:
%
%    The Delannoy number A(M,N) counts the number of distinct paths
%    from (0,0) to (M,N) in which the only steps used are
%    (1,1), (1,0) and (0,1).
%
%    This also counts the number of distinct paths a chess king could
%    take, going only up, right, or diagonally up and right, from (0,0)
%    to (M,N).
%
%    This also counts the number of possible alignments between two
%    sequences, one with M base pairs and one with N.  The alignment
%    can introduce a gap in the first sequence, or the second sequence,
%    or match the currently considered base pairs.
%
%  First values:
%
%      \N 0  1   2   3    4     5     6      7      8
%     M-+--------------------------------------------
%     0 : 1  1   1   1    1     1     1      1      1
%     1 : 1  3   5   7    9    11    13     15     17
%     2 : 1  5  13  25   41    61    85    113    145
%     3 : 1  7  25  63  129   231   377    575    833
%     4 : 1  9  41 129  321   681  1289   2241   3649
%     5 : 1 11  61 231  681  1683  3653   7183  13073
%     6 : 1 13  85 377 1289  3653  8989  19825  40081
%     7 : 1 15 113 575 2241  7183 19825  48639 108545
%     8 : 1 17 145 833 3649 13073 40081 108545 265729
%
%  Recursion:
%
%    A(0,0) = 1
%    A(M,0) = 1
%    A(0,N) = 1
%    A(M,N) = A(M-1,N) + A(M,N-1) + A(M-1,N-1)
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    06 July 2026
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    John D Cook,
%    DNA Sequence Alignment and Kings,
%    https://www.johndcook.com/blog/2026/06/30/dna-sequence-alignment-and-kings/
%    Posted 30 June 2026.
%
%    Eric Weisstein,
%    CRC Concise Encyclopedia of Mathematics,
%    CRC Press, 2002,
%    Second edition,
%    ISBN: 1584883472,
%    LC: QA5.W45
%
%  Input:
%
%    integer N_DATA: The user sets N_DATA to 0 before the first call.  
%    Thereafter, it should simply be the value returned by the previous call.
%
%  Output:
%
%    integer N_DATA: On each call, the routine increments N_DATA by 1, and
%    returns the corresponding data; when there is no more data, the
%    output value of N_DATA will be 0 again.
%
%    integer M, N: the indices.
%
%    integer D: the Delannoy number D(M,N).
%
  n_max = 21;

  m_vec = [ 0, ...
            1, 1, 1, ...
            2, 2, 2, 2, ...
            3, 3, 3, 3, ...
            4, 4, 4, 4, ...
            5, 6, 7, 8, ...
            12 ];

  n_vec = [ 0, ...
            0, 1, 2, ...
            2, 3, 4, 5, ...
            5, 6, 7, 8, ...
            5, 6, 7, 8, ...
            5, 6, 7, 8, ...
           15 ];

  d_vec = [         1, ...
                    1,    3,     5, ...
                    5,    7,     9,     11, ...
                  231,  377,   575,    833, ...
                  681, 1289,  2241,   3649, ...
                 1683, 8989, 48639, 265729, ...
           2653649025 ];

  if ( n_data < 0 )
    n_data = 0;
  end

  n_data = n_data + 1;

  if ( n_max < n_data )
    n_data = 0;
    m = 0;
    n = 0;
    d = 0;
  else
    m = m_vec(n_data);
    n = n_vec(n_data);
    d = d_vec(n_data);
  end

  return
end
