Magic squares of doubly even order: Difference between revisions
Content deleted Content added
→{{header|Haskell}}: (fixed error in previous edit) |
→{{header|Haskell}}: (added test) |
||
Line 282:
=={{header|Haskell}}==
<lang Haskell>
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
-- 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
[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]
main2
(260,True)</pre>
=={{header|Java}}==
|