Let us say that a magic square is a 4x4-square grid of numbers 1,…,16 in some order such that the sum of numbers of every horizontal row or vertical column is constant. How can I make the program to find nth magic square for given n? Here, the nth means that if we replace 1 by A, 2 by B and so on and we read the square from left to right, up to bottom, we order the word by alphabetical order. Example

1 2 15 16

6 11 7 10

13 12 4 5

14 9 8 3

corresponds ABOPFKGJMLDENIHC

and

1 2 15 16

6 11 7 10

14 9 8 3

13 12 4 5

corresponds ABOPFKGJNIHCMLDE so ABOPFKGJMLDENIHC comes before ABOPFKGJNIHCMLDE.