function derange1_back_next_test ( )

%*****************************************************************************80
%
%% derange1_back_next_test() tests derange1_back_next().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    19 June 2015
%
%  Author:
%
%    John Burkardt
%
  n = 5;
  fprintf ( 1, '\n' );
  fprintf ( 1, 'derange1_back_next_test():\n' );
  fprintf ( 1, '  derange1_back_next() generates derangements\n' );
  fprintf ( 1, '  using backtracking.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Here, we seek all derangments of order N = %d\n', n );
  fprintf ( 1, '\n' );

  a = [];
  more = 0;
  rank = 0;

  while ( true )

    [ a, more ] = derange1_back_next ( n, a, more );

    if ( ~more )
      break
    end

    rank = rank + 1;
    fprintf ( 1, '  %2d', rank );
    for i = 1 : n
      fprintf ( 1, '  %2d', a(i) );
    end
    fprintf ( 1, '\n' );

  end

  return
end
