Cody

Problem 44450. Create a magic square matrix for a given odd integer

Solution 3157386

Submitted on 11 Oct 2020 by Rafael S.T. Vieira
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
n = 3; y_correct = [15 15 15 15]; %row sum, col sum, main diag sum, other diag sum [a b c d] = MagicSquare(n); assert(isequal([a b c d],y_correct))

M = 6 1 8 7 5 3 2 9 4

2   Pass
n = 5; y_correct = [65 65 65 65]; %row sum, col sum, main diag sum, other diag sum [a b c d] = MagicSquare(n); assert(isequal([a b c d],y_correct))

M = 15 8 1 24 17 16 14 7 5 23 22 20 13 6 4 3 21 19 12 10 9 2 25 18 11

3   Pass
n = 9; y_correct = [369 369 369 369]; %row sum, col sum, main diag sum, other diag sum [a b c d] = MagicSquare(n); assert(isequal([a b c d],y_correct))

M = 45 34 23 12 1 80 69 58 47 46 44 33 22 11 9 79 68 57 56 54 43 32 21 10 8 78 67 66 55 53 42 31 20 18 7 77 76 65 63 52 41 30 19 17 6 5 75 64 62 51 40 29 27 16 15 4 74 72 61 50 39 28 26 25 14 3 73 71 60 49 38 36 35 24 13 2 81 70 59 48 37

4   Pass
n = 15; y_correct = [1695 1695 1695 1695]; %row sum, col sum, main diag sum, other diag sum [a b c d] = MagicSquare(n); assert(isequal([a b c d],y_correct))

M = 120 103 86 69 52 35 18 1 224 207 190 173 156 139 122 121 119 102 85 68 51 34 17 15 223 206 189 172 155 138 137 135 118 101 84 67 50 33 16 14 222 205 188 171 154 153 136 134 117 100 83 66 49 32 30 13 221 204 187 170 169 152 150 133 116 99 82 65 48 31 29 12 220 203 186 185 168 151 149 132 115 98 81 64 47 45 28 11 219 202 201 184 167 165 148 131 114 97 80 63 46 44 27 10 218 217 200 183 166 164 147 130 113 96 79 62 60 43 26 9 8 216 199 182 180 163 146 129 112 95 78 61 59 42 25 24 7 215 198 181 179 162 145 128 111 94 77 75 58 41 40 23 6 214 197 195 178 161 144 127 110 93 76 74 57 56 39 22 5 213 196 194 177 160 143 126 109 92 90 73 72 55 38 21 4 212 210 193 176 159 142 125 108 91 89 88 71 54 37 20 3 211 209 192 175 158 141 124 107 105 104 87 70 53 36 19 2 225 208 191 174 157 140 123 106

Suggested Problems

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!