1

I am trying to solve a magic square problem with some modification. I have an array of 25 non-consecutive integers, and I have to arrange them in 5x5 2D array so that this array forms a magic square. I have tried brute force algorithm but it is consuming too much time. I tried searching on the net for modifications of magic square problem but I found nothing for random non-consecutive numbers. Please let me know if there is any better way to solve this problem.

Thanks,

SSMA
  • 497
  • 1
  • 7
  • 19
  • I think there is already quite some code out there: e.g. [here](http://www.guideforschool.com/1280095-java-program-to-print-magic-square-matrix/) or [here](http://introcs.cs.princeton.edu/java/14array/MagicSquare.java.html) or even [here](http://stackoverflow.com/questions/14574079/magic-square-program-help-java). Don't these help you? – Mr Tsjolder from codidact May 28 '15 at 06:23
  • No, this does not help me. The first 2 links create magic squares for consecutive numbers. I have non-consecutive. The third link checks if a square is magic square or not, I have 25 integers and I will have to check all possible combinations of these 25 integers to check if one of them makes a magic square. I have already tried that and it takes too much time. – SSMA May 28 '15 at 10:32
  • You should not check all the possible combinations. If the sum of each row should be the same, then it should be equal to 1/5 of total elements sum. Just sum all the elements divide by 5 (if not divisible, then problem is unsolveable). Then you only have to brute force `C(25,5)` combintations which much less than `25!` – Tagir Valeev May 28 '15 at 10:39
  • I'm voting to close this question as off-topic because "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." – Gangnus May 28 '15 at 12:00
  • @SSMA I wish to apologize, I read consecutive. I think the comment of Valeev should be quite helpful... – Mr Tsjolder from codidact May 28 '15 at 14:14
  • @Gangnus This is not homework help. And I have stated that I have tried brute force and it takes too much time, and I have checked algorithms for consecutive numbers but they do not help me. However, the suggestion of Valeev is really good and I will be working on it to check how much time it consumes. – SSMA May 28 '15 at 14:53
  • @SSMA. OK, I retracted my close vote. I am sorry. – Gangnus May 28 '15 at 15:54

0 Answers0