Basically for an assignment I need to create a C# program that will take a number as input (n) then create a 2D array size n*n with the numbers 1 to (n*n). It needs to use a brute force method. I have done this but at the moment the program will just randomly generate the order of the numbers each time, so it will sometimes check the same order more than once. Obviously this means it takes a really long time to check any number above 3, and even for 3 it can take a few minutes. Basically I'm wondering if there is any way for me to make it only check each order once. I am only allowed to use "basic" C# functions, so just things like *, /, +, - and nothing like .Shuffle etc.
-
Can you write your own shuffle function? It's really easy to do. – Bernhard Barker Jan 19 '14 at 17:18
-
1) shows us what you have so far, and 2) what do you mean by Brute Force, and why must you use it? 3) If you really mean true Brute Force (exhaustive search of all possible combinations), it obviously has severe exponential growth for this problem, even three should be slow. – RBarryYoung Jan 19 '14 at 17:19
-
Based on what you have done I assume that these numbers has to be randomly distributed in the grid (not just starting from 1 in the top left). Is this correct? – Bernhard Barker Jan 19 '14 at 17:21
-
What do you mean when you use the word "order"? – dursk Jan 19 '14 at 17:22
-
@Dukeling yeah, I have a function to create the array, one to shuffle it, one to put the numbers into the a 2D array and one to check if all the sums of the rows, columns and main diagonals are equal. My main program at the moment just runs all the functions in a row then if the check sums function returns false then it runs them all again. – user3193861 Jan 19 '14 at 17:33
-
@RBarryYoung I do mean actual brute force, checking each possible combination until the right one is found. It only needs to work and will only be tested with 3, but in theory it should be able to work with any number if left long enough. – user3193861 Jan 19 '14 at 17:35
-
"... and one to check if all the sums of the rows, columns and main diagonals are equal" - I think you forgot to mention this constraint in the question. – Bernhard Barker Jan 19 '14 at 18:23
2 Answers
Let me make sure I understand the question: you wish to enumerate all permutations of the numbers 1 through n squared, and check whether the permutation produces a magic square. You are now generating permutations randomly, but instead you wish to generate all permutations.
I wrote a series of articles on generating permutations; it is too long to easily summarize here.
http://ericlippert.com/2013/04/15/producing-permutations-part-one/

- 647,829
- 179
- 1,238
- 2,067
Choosing random order, as you found, is not a good idea.
I suggest that you put all the number 1 ... (n*n) in array , and than find all the permutation.
when you have all the permutation, it's easy to create square (1 .. n ==> the first row, n+1 ... 2n ==> the second row and so on).
Now, finding all the permutation can be done with the basic operation with recursion

- 5,210
- 2
- 24
- 37