function counts = candy_count_box ( c, l, m, n )

%*****************************************************************************80
%
%% candy_count_box() counts candy types in an LxMxN box.
%
%  Discussion:
%
%    We are given a box of candy containing C distinct types, and
%    asked to report how many of each type there are.
%
%    In this case, the box is an L by M by N matrix represented as A(I,J,K).  
%
%    The box entry in row I, column J, level K will store candy type C, where
%    A(i,j,k) = mod ( I + J + K - 3, C ) + 1.
%    (If we start I, J, K and C indexing at 0, this simplifies to
%    mod(I+J+K,C)! )
%
%    The effect of this numbering scheme is that the candy type is
%    constant along diagonal lines and sheets.
%
%    The task is to determine, for a given set of C, L, M and N,
%    the number of candies of each type.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 June 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer c: the number of types of candy.
%
%    integer l, m, n: the number of rows, columns, and levels in the candy box.
%
%  Output:
%
%    integer counts(c): the number of each type of candy in the box.
%
  counts = zeros ( c, 1 );
%
%  Do the computation for the first level:
%
  counts_2d = candy_count_matrix ( c, m, n );
%
%  L = LF * C + LR
%
  lf = floor ( l / c );
  lr = l - lf * c;
%
%  Part of the box can be regarded as LF repetitions 
%  of a stack of sheets for which the item in the (K,1,1) position is 
%  successively 1, 2, ..., C.
%
  counts = zeros ( c, 1 );
  for k = 1 : c
    counts(1:c) = counts(1:c) + lf * circshift ( counts_2d(1:c), k - 1 );
  end
%
%  Now there are 0 <= LR < C more sheets, for which the item in the (K,1,1) 
%  position successively is 1, 2, ..., LR.
%  
  for k = 1 : lr
    counts(1:c) = counts(1:c) + circshift ( counts_2d(1:c), k - 1 );
  end

  return
end

