If consecutive natural numbers 1 .. n^2 are placed in a n*n grid so that sum of numbers in each column and in each row is a constant then we call it a magic square. I would like to generate magic squares of size 16x16 and 256x256 for not skewed byte mapping. Maps arranged as magic squares tend to have better distribution properties. Algo should produce a map that depends on the seed, rather than yield the same result each time.
expectedSum = ((1+n*n)*n)/2). I can shuffle an array using some form of SecureRandom that accepts a seed. Then in case of a small square I can go for a brute force for loop.I can just brute force 3x3 magic square. Considering symmetries I need to check less than 8*(7!) or about 40k possibilities.
This approach wouldn't work for 16x16 board because 256! > 10^500 is quite large. My best guess is to find two row_column pairs such that sums along row and column are greater than the expected sum in first pair and lower in second pair. If shared element in first pair is greater than in the second pair - swap elements. This algo seems better than bruteforce (can complete in reasonable time), but there is no guarantee that it halts. Consider 2x2 square to see how it fails.
What is a good Monte-Carlo approach to solve a large magic square?