  n = 4;
  p = zeros(n,1);
  rank = 0;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Rank:  Permutation\n' );
  fprintf ( 1, '\n' );

  while ( 1 )

    p = next_perm ( p );

    if ( isempty ( p ) )
      fprintf ( 1, '\n' );
      fprintf ( 1, '  That was the last permutation.\n' );
      break
    end

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

  end
