I'm looking for a formula or rule that will allow me to distribute n characters into a n*n grid with as perfect of a distribution as possible. Let's say we have an array of 5 characters, A through E. Here's an example of how it definitely shouldn't turn out:
A B C D E
B C D E A
C D E A B
D E A B C
E A B C D
With this pattern, the letters clump together and are not evenly spaced. See for example the diagonal lines with repeating E's and A's etc. If we were to try another pattern however:
A B C D E
D E A B C
B C D E A
E A B C D
C D E A B
Now with this pattern, all the characters are evenly spaced along the grid. For example, you're not likely to find the letter B all that close to another B on either axis.
I'm hoping there's a rule here as to how you should shift the the A B C D E configuration around for each row to produce a spread out pattern, so I can use it to calculate not only small arrays such as this one, but arrays of any size. Any ideas as to how this can be accomplished? The language I'm trying to do this in is Objective-c if that helps.
Update: This grid is using one of the rules suggested in the answers. Unfortunetly what we see is very distinct diagonal lines. For example, the does not spread out evenly along the grid.
B C D E F G H I J K L M N O P Q R S
J K L M N O P Q R S B C D E F G H I
S B C D E F G H I J K L M N O P Q R
I J K L M N O P Q R S B C D E F G H
R S B C D E F G H I J K L M N O P Q
H I J K L M N O P Q R S B C D E F G
Q R S B C D E F G H I J K L M N O P
G H I J K L M N O P Q R S B C D E F
P Q R S B C D E F G H I J K L M N O
F G H I J K L M N O P Q R S B C D E
O P Q R S B C D E F G H I J K L M N
E F G H I J K L M N O P Q R S B C D
N O P Q R S B C D E F G H I J K L M
D E F G H I J K L M N O P Q R S B C
M N O P Q R S B C D E F G H I J K L
C D E F G H I J K L M N O P Q R S B
L M N O P Q R S B C D E F G H I J K
B C D E F G H I J K L M N O P Q R S
K L M N O P Q R S B C D E F G H I J
Update2: Another version of the grid using another set of rules suggested. Similar to the example above, the characters are indeed spaced far away from each other but only on the x-axis.
B C D E F G H I J K L
G H I J K L B C D E F
B C D E F G H I J K L
G H I J K L B C D E F
B C D E F G H I J K L
G H I J K L B C D E F
B C D E F G H I J K L
G H I J K L B C D E F
B C D E F G H I J K L
G H I J K L B C D E F
B C D E F G H I J K L
G H I J K L B C D E F
Update3: Moron's suggestion seems to have done the trick (plase note his addition of using a coprime to n that's near sqrt(n)). Here's a grid plotted thanks to his answers.
B C D E F G H I J K L M N O P Q R S
P Q R S B C D E F G H I J K L M N O
L M N O P Q R S B C D E F G H I J K
H I J K L M N O P Q R S B C D E F G
D E F G H I J K L M N O P Q R S B C
S B C D E F G H I J K L M N O P Q R
O P Q R S B C D E F G H I J K L M N
K L M N O P Q R S B C D E F G H I J
G H I J K L M N O P Q R S B C D E F
C D E F G H I J K L M N O P Q R S B
R S B C D E F G H I J K L M N O P Q
N O P Q R S B C D E F G H I J K L M
J K L M N O P Q R S B C D E F G H I
F G H I J K L M N O P Q R S B C D E
B C D E F G H I J K L M N O P Q R S
Q R S B C D E F G H I J K L M N O P
M N O P Q R S B C D E F G H I J K L
I J K L M N O P Q R S B C D E F G H
E F G H I J K L M N O P Q R S B C D