943,613 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3182
  • Java RSS
Jan 12th, 2009
0

even magic square?

Expand Post »
Hi there,
this is my first post, so pls be polite
since a few days i am programming java (for school).

but i dont get it.. i tried to fill the 4x4 matrix with random generated numbers (1-16) and wrote a method, that they could only be used once.
then i wrote some methods to check wether die rows,cols and diagonals have the right sum.

But that way coasts me ~1 day to find a real magic square and that is def. not okay.
i googled for a while and found many solutions, but most of them didn't work correctly. someone told me, that i am supposed to use "backtracking". but i dont understand this theory.

Does anybody have a working backtracking solution for an evan magic square or could explain that theory to me?

it would be really great!

thank you

bye


ps: sorry for my bad english.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingarthur is offline Offline
13 posts
since Jan 2009
Jan 13th, 2009
0

Re: even magic square?

I'd like to help... but I have no knowledge of magic squares or the mentioned algorithm... or perhaps I do and it just sounds foreign. Care to explain what a magic square is? An example would suffice. It appears that you just need a bunch of criteria to verify if the square is 'magic' or not? In which case, please show us your code so that we can identify logic errors... and perhaps you don't need to use backtracking.

Oh and welcome to DaniWeb! You'll find that we're rather polite and friendly to those that actually try to do their homework hehe
Last edited by PoovenM; Jan 13th, 2009 at 4:58 pm.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Jan 14th, 2009
0

Re: even magic square?

Hi,
thank you for welcoming me


Sorry, i'd forgotten to explain the magic square:
a magicS is matrix (in my case 4x4) filled with the numbers from 1 - 16. each number could only be used once and the sum of every row,collum and the 2 diagonals has to be 34.

i have tried some algorithms and i made it.
java Syntax (Toggle Plain Text)
  1. public void versuche(int n, int[][] quadrat, int ziffer){
  2. long time1 = System.currentTimeMillis();
  3. if ((ziffer > (n*n)) && isMagisch(quadrat)){
  4. zeige(quadrat);
  5. System.exit(0);
  6. } else {
  7. for (int x = 0;x<n; x++){
  8. for (int y = 0; y <n; y++){
  9. if (quadrat[x][y] == 0){
  10. quadrat[x][y] = ziffer;
  11. do{
  12. versuche(n, quadrat, ziffer);
  13. quadrat[x][y] = 0;
  14. }while(checkZiffer(quadrat, ziffer));
  15. }
  16. }
  17. }
  18. }
  19. }

but this one is very slowly. it should solve the square in 1-2 seconds. my algorithm needs 30min


Could someone help here?
Last edited by kingarthur; Jan 14th, 2009 at 4:51 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingarthur is offline Offline
13 posts
since Jan 2009
Jan 14th, 2009
0

Re: even magic square?

Oh gosh, your code is rather confusing... I think it's because I'm not sure what your methods and variable are meant to do. If the algo is taking that long to compute the answer (does it give the correct answer?) then it is flawed.
My first question: the method isMagisch, what does that check? Based on the name, I'd assume it determines if the matrix is 'magic' or not?
My second question: How do you verify that the numbers you generate are not duplicated?

My suggestion: Sum the diagonal that goes from left to right in which case the indices increment by 1 for both dimensions of the matrix, that is, [i][i] where i is the counter in the for loop.
Then sum the other diagonal, where one dimension increases as the other decreases (the pairs would be {0, 2}; {1, 1}; {2, 0}). If the sum of the previous diagonal is the same as the newly calculated sum then proceed.
Sum each row, iteratively, if the sum of the diagonal isn't equal at any point then break the loop.
Sum each column and the same principal as above applies. Don't sum each column if the above checks indicated a negative 'magic square.'

If you're having trouble understanding how to implemented what I've suggested, please first try parts of it and post your attempted code and we'll help spruce it up. My suggested method might not be 'backtracking' but it should take at most a few seconds to process.

Oh, one last comment: it seems that you're using n to store the dimension of your matric. Rather use quadrat.length or quadrat[0].length where both length final variables would have the same value. Let me know if you don't understand this concept.
Last edited by PoovenM; Jan 14th, 2009 at 5:26 am.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Jan 14th, 2009
0

Re: even magic square?

Did you research the problem at all before diving headlong into the code?
http://en.wikipedia.org/wiki/Magic_square
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 14th, 2009
0

Re: even magic square?

Ah, so then calculate M, as defined by the wikipedia site Salem provided and ensure the calculated sums match M?
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Jan 14th, 2009
0

Re: even magic square?

Click to Expand / Collapse  Quote originally posted by Salem ...
Did you research the problem at all before diving headlong into the code?
http://en.wikipedia.org/wiki/Magic_square
What do you exactly mean?
the first part with M? this is definitly wrong. just try n = 4.

in their set phrase the Sum should be 43. but it isnt. a magic square of n=4 has a sum of 34(!!). the real set phrase has to be :
M = n/2 * (n+1)
-> n means cells (=16)
M = 16/2 * 17
M = 8 * 17
M = 136
136 : 4 = 34

if you meant the constructing part, then: this doesnt work neither. because i have initial values, so i cant work with a system. i have to "backtrack"

My hole programm runs correctly. if n=3, then it take me just 250ms to generate the square. but with n=4 i need 3h(!!).

Quote ...
Oh gosh, your code is rather confusing... I think it's because I'm not sure what your methods and variable are meant to do. If the algo is taking that long to compute the answer (does it give the correct answer?) then it is flawed.
My first question: the method isMagisch, what does that check? Based on the name, I'd assume it determines if the matrix is 'magic' or not?
My second question: How do you verify that the numbers you generate are not duplicated?
isMagisch checks the entire square (collums,rows and diagonales).
to verify i wrote a method checkNum(), which goes threw the array and checks wether the number is used or not. then it returns true or false
if its true (== number is already justed) then he adds 1.

Quote ...
My suggestion: Sum the diagonal that goes from left to right in which case the indices increment by 1 for both dimensions of the matrix, that is, [i][i] where i is the counter in the for loop.
Then sum the other diagonal, where one dimension increases as the other decreases (the pairs would be {0, 2}; {1, 1}; {2, 0}). If the sum of the previous diagonal is the same as the newly calculated sum then proceed.
okay, you mean, that i have to check only the diagonales, while i am filling it?!
what should it be good for? it doesnt proof anything

Quote ...
Oh, one last comment: it seems that you're using n to store the dimension of your matric. Rather use quadrat.length or quadrat[0].length where both length final variables would have the same value. Let me know if you don't understand this concept.
stupid question, but why? where is the difference? i tought it would be easier for the "user"

thanks for your patience
Last edited by kingarthur; Jan 14th, 2009 at 5:19 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingarthur is offline Offline
13 posts
since Jan 2009
Jan 14th, 2009
0

Re: even magic square?

What part of your code isn't working? Your code should be split into pieces/methods that each perform a specific task. You should be able to identify where the code isn't behaving as expected, either with a debugger or just by using print statements. Tell us where your code doesn't work, and we will help you make it work. Asking us to "solve this for you" is ridiculous, especially since very few of us actually know what magic squares is.
Last edited by BestJewSinceJC; Jan 14th, 2009 at 11:40 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jan 15th, 2009
0

Re: even magic square?

Hi there,

sorry. i didn't ment, that you should solve my homework

Okay, from the scratch^^ :

a magic square is a square with a length of 4x4 Cells (in my case).
these cells should be filled with the numbers 1-16. each number should only be used once and the sum of each row, collum and diagonal has to be 34.


My version of the magic square is working. but it costs me 3h, instead of 1-2 seconds.

my code is splitted into methods for: filling the square, for checking the entire square of it's sums, for checking the current number, if its already used and for printing the whole array.

its just, that the filling seems to have some problems. i tought, that their is a general solution for backtracking and i made a clearly mistake that you would see in a view seconds

all my other methods are working fine. i tested them.


bye
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingarthur is offline Offline
13 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help in classes
Next Thread in Java Forum Timeline: Requiring programs in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC