Determine the magic sum from a magic square
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Farooq
il 22 Set 2022
Commentato: Nathan Hardenberg
il 25 Lug 2023
Determine for a magic square of order n, the magic sum m. For example m=15 for a magic square of order 3.
For this problem I wrote:
m = sum(n)
This gave me 15 15 15. Why is this not correct?
I then tried
m = sum(n(1, :))
This gave me 15. Why is this not correct?
I don't understand why my answer is not accepted. This is for Cody Problem 1087: Magic is simple (for beginners).
1 Commento
David Goodmanson
il 22 Set 2022
Hi Farook,
It looks like you are given the size of the magic square, but not the magic square itself. So if all you are given is that the magic square is 3x3, how do you obtain 15 from n=3?
Risposta accettata
Davide Masiello
il 22 Set 2022
Modificato: Davide Masiello
il 22 Set 2022
I think you are inputting the magic square itself to your function, rather than its order.
I believe the relation between a magic square order (n) and the magic sum (S) is
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1133235/image.png)
So your function should be
function out = magic_sum(n)
out = n*(n^2+1)/2
end
2 Commenti
Nathan Hardenberg
il 25 Lug 2023
While the formula is a valid solution I think the intended way is to sum up one colum or row, since all rows/colums and diagonals do sum up to the same number:
mag_sqr = magic(3)
sol1 = sum(mag_sqr(:,1)) % sum up first colum
sol2 = sum(mag_sqr(1,:)) % sum up first row
Più risposte (0)
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!