Magic squares of doubly even order: Difference between revisions

→‎{{header|Haskell}}: dropped splitEvery for chunksOf, updated output
No edit summary
(→‎{{header|Haskell}}: dropped splitEvery for chunksOf, updated output)
Line 637:
=={{header|Haskell}}==
 
<lang Haskell>import Data.List.Split (transpose, unfoldrchunksOf)
import Data.List (transpose, unfoldr)
 
magicSquare :: Int -> [[Int]]
Line 643 ⟶ 644:
| rem n 4 > 0 = []
| otherwise =
splitEverychunksOf n $
-- Taken directly from the integer series where True
-- and from the reverse of that series where False
Line 663 ⟶ 664:
-- minimum truth table, horizontally and vertically
| otherwise =
concat . concat . concat . scale $ scale <$> chunksOf 4 (magicSeries 4)
scale <$> splitEvery 4 (magicSeries 4)
where
scale = replicate $ quot n 4
 
------------------------------------------------------------------------
magicSeries :: Int -> [Bool]
magicSeries = (iterate (\xs -> xs (++ (not) <$*> xs)fmap not) [True] !!)
 
splitEvery :: Int -> [a] -> [[a]]
splitEvery n = takeWhile (not . null) . unfoldr (Just . splitAt n)
 
isPowerOf :: Int -> Int -> Bool
isPowerOf k n = until (\x(0 ->/=) . flip rem x k /= 0) (`quot` k) n == 1
 
splitEverydiagonals :: [[Int -> [a]] -> [[aInt]]
main :: IO ()
diagonals xs = fmap (flip (zipWith (!!)) [0 ..]) . ((:) <$*> [xs,(return reverse. xs]reverse))
main = mapM_ print $ magicSquare 8
 
-- Summed and checked---------------------------------------------------
checked :: Int -> (Int, Bool)
checked n = (h, all (h ==) t)
Line 692 ⟶ 687:
diagonals square -- diagonals
 
main :: IO ()
diagonals :: [[Int]] -> [[Int]]
main = do
diagonals xs = flip (zipWith (!!)) [0 ..] <$> [xs, reverse xs]
main = mapM_ print $ magicSquare 8
 
putStrLn ""
main2 :: IO ()
main2 = print $ checked 8</lang>
 
{{Out}}
<pre>[1,63,62,4,60,6,7,57]
<pre>main
[156,6310,6211,453,513,5951,5850,816]
[5648,1018,1119,5345,5221,1443,1542,4924]
[4825,1839,1938,4528,4436,2230,2331,4133]
[2532,3934,38,2835,29,3537,3427,3226,40]
[3341,3123,3022,3644,3720,2746,2647,4017]
[2449,4215,4314,2152,2012,4654,4755,179]
[168,5058,5159,135,1261,543,552,964]
[57,7,6,60,61,3,2,64]
 
main2
(260,True)</pre>
 
9,659

edits