Skip to content

Instantly share code, notes, and snippets.

@deB4SH
Created April 5, 2016 06:29
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 deB4SH/e68be82e0c0413b94ee499244ff3cbb4 to your computer and use it in GitHub Desktop.
Save deB4SH/e68be82e0c0413b94ee499244ff3cbb4 to your computer and use it in GitHub Desktop.
;A 3x3 magic square is a 3x3 grid of the numbers 1-9 such that each row, column, and major diagonal adds up to 15.
;[8, 1, 6, 3, 5, 7, 4, 9, 2] => true
;[2, 7, 6, 9, 5, 1, 4, 3, 8] => true
;[3, 5, 7, 8, 1, 6, 4, 9, 2] => false
;[8, 1, 6, 7, 5, 3, 4, 9, 2] => false
(defn testMagicSquare [a1 a2 a3 b1 b2 b3 c1 c2 c3]
(if (= (+ (+ a1 a2) a3) 15)
(if (= (+ (+ b1 b2) b3) 15)
(if (= (+ (+ c1 c2) c3) 15)
(if (= (+ (+ a1 b1) c1) 15)
(if (= (+ (+ a2 b2) c2) 15)
(if (= (+ (+ a3 b3) c3) 15)
(if (= (+ (+ a1 b2) c3) 15)
(if (= (+ (+ a3 b2) c1) 15)
(print true) (print false)
)(print false)
)(print false)
)(print false)
)(print false)
)(print false)
)(print false)
)(print false)
)
)
(testMagicSquare 8 1 6 3 5 7 4 9 2)
(testMagicSquare 2 7 6 9 5 1 4 3 8)
(testMagicSquare 3 5 7 8 1 6 4 9 2)
(testMagicSquare 8 1 6 7 5 3 4 9 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment