Problem 151. Magic!
Solution Stats
Problem Comments

5 Comments
This could be improved with a better test suite. For example, flipud(magic(n)) is still magical, but most of the leading solutions would return false for this.
Yes, test suite is substandard. Could have drawn from this: http://nrich.maths.org/1338
Why is magic(2) not a magic square ?
I was not notified about some of the older comments, so was unaware of some of the weaknesses of the test suite, and the "gamed" solutions. I have substantially improved the test suite.
The MATLAB command magic(2), although it does produce output, is not a magic square, according to the definition. (There is no 2x2 magic square.)
Solution Comments

2 Comments
x = magic(2);
y_correct = false;
This is wrong!!!! hence solution failed
Although MATLAB does create a 2x2 matrix as an output from magic(2), that output is not a magic square.

2 Comments
I'm pretty sure test 5 is wrong. magic(3)+1: sum of every row, column, and both diagonals is 18
It's not a normal magic square if it's of size 3x3 and doesn't sum to 15. All rows/cols/diags must sum to n*(n^2+1)/2.

1 Comment
I don't think this checks the diagonals. Is that because it's not necessary?

2 Comments
issame is from which toolbox ?
($matlabroot)/toolbox/robust/rctobsolete/robust/issame.m

1 Comment
passed in matlab but not here??what ??

2 Comments
Test 4 is wrong: y_correct should be = TRUE
No, test 4 is correct. magic(2) does not return a magic square.

2 Comments
Hein? 348052801600 where does this number come from?!
Note that this gives the wrong result for magic(4). I suspect that this is tailored to the test set.

1 Comment
I like this since there is nothing in the MATLAB doc which guarantees that magic() produces a normal magic square. Is it necessary to also check both diagonals?

1 Comment
Folks, I don't get it. If you create z= magic(3)', that's a magic square. No surprise. If you then do an isequal with magic(3), you fail the test. Thus this and all solutions of this class are wrong, marked as correct only because the tests don't offer transposed magic squares as a test. The test vector needs to be fixed so all these solutions show us as not working.
Problem Recent Solvers283
Suggested Problems

1385 Solvers

Increment a number, given its digits
595 Solvers

Project Euler: Problem 6, Natural numbers, squares and sums.
1528 Solvers

586 Solvers

345 Solvers
More from this Author23
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!