code: private int[] unrankPermutationLexicographically(int rank, int size) {
          int[] permutation = new int[size + 1];
          permutation[size] = 1;
          for (int j = 1; j < size; j++) {
              int d = (rank % factorial(j + 1)) / factorial(j);
              rank = rank - d * factorial(j);
              permutation[size - j] = d + 1;
              for (int i = size - j + 1; i <= size; i++) {
                  if (permutation[i] > d) {
                      permutation[i]++;
                  }
              }
          }
  
          // convert an array of numbers like [1...n] to [0...n-1]
          int[] shiftedPermutation = new int[size];
          for (int i = 1; i < permutation.length; i++) {
              shiftedPermutation[i - 1] = permutation[i] - 1;
          }
          return shiftedPermutation;
      }
docs: Performs the opposite to the rank method, producing the permutation that
  has the order <code>rank</code> in the lexicographically ordered list.
  
  As an implementation note, the algorithm assumes that the permutation is
  in the form [1,...N] not the more usual [0,...N-1] for a list of size N.
  This is why there is the final step of 'shifting' the permutation. The
  shift also reduces the numbers by one to make them array indices.
  
  @param rank the order of the permutation to generate
  @param size the length/size of the permutation
  @return a permutation as an int array