Skip to content

Instantly share code, notes, and snippets.

@shortsightedsid
Created April 10, 2016 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shortsightedsid/22c8cc29a38746af1156b17c282956bc to your computer and use it in GitHub Desktop.
Save shortsightedsid/22c8cc29a38746af1156b17c282956bc to your computer and use it in GitHub Desktop.
Check if a 2D Vector is a Magic Square
;;; Magic Squares - https://www.reddit.com/r/dailyprogrammer/comments/4dccix/20160404_challenge_261_easy_verifying_3x3_magic/
(defun magic-square? (square)
(let* ((dims (array-dimensions square))
(size (car dims)))
(if (and (typep square 'simple-array)
(eq 2 (array-rank square))
(eq size (cadr dims)))
(loop for i from 0 below size
for j from 0 below size
summing (aref square i j) into lr
summing (aref square i (- size j 1)) into rl
finally (return (eq lr rl)))
nil)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment