Magic squares of doubly even order: Difference between revisions

(→‎{{header|Haskell}}: (fixed error in previous edit))
(→‎{{header|Haskell}}: (added test))
Line 282:
=={{header|Haskell}}==
 
<lang Haskell>magicSquareimport ::Data.List Int -> [[Int]](transpose)
 
magicSquare :: Int -> [[Int]]
magicSquare n
| (rem n 4) > 0 = []
Line 309 ⟶ 311:
where
(gp, t) = splitAt n xs
 
main :: IO ()
main = mapM_ (putStrLn . show) $ magicSquare 8</lang>
 
 
-- Summed and checked
checked :: Int -> (Int, Bool)
checked n = (h, all (\x -> h == x) t)
where
square = magicSquare n
sums@h:t = sum <$> (square) ++ -- rows
(transpose square) ++ -- cols
(diagonals square) -- diagonals
diagonals :: [[Int]] -> [[Int]]
diagonals xs =
map (\x -> zipWith (!!) x [0..]) [xs, reverse xs]
main2 :: IO ()
main2 = putStrLn $ show (checked 8)</lang>
 
{{Out}}
<pre>main
<pre>[1,63,62,4,5,59,58,8]
[56,10,11,53,52,14,15,49]
[48,18,19,45,44,22,23,41]
Line 323 ⟶ 342:
[24,42,43,21,20,46,47,17]
[16,50,51,13,12,54,55,9]
[57,7,6,60,61,3,2,64]</pre>
 
main2
(260,True)</pre>
 
=={{header|Java}}==
9,659

edits