Magic squares of doubly even order: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed wording in the REXX section header.) |
m (→{{header|Haskell}}: (pruning some redundant brackets )) |
||
Line 567: | Line 567: | ||
<lang Haskell>import Data.List (transpose, unfoldr) |
<lang Haskell>import Data.List (transpose, unfoldr) |
||
magicSquare :: Int -> [[Int]] |
magicSquare :: Int -> [[Int]] |
||
magicSquare n |
magicSquare n |
||
| |
| rem n 4 > 0 = [] |
||
| otherwise = splitEvery n $ |
| otherwise = splitEvery n $ |
||
-- Taken directly from the integer series where True |
-- Taken directly from the integer series where True |
||
Line 583: | Line 583: | ||
-- For integer powers of 2, the (append not) 'magic' series directly |
-- For integer powers of 2, the (append not) 'magic' series directly |
||
-- yields the truth table that we need |
-- yields the truth table that we need |
||
| isPowerOf 2 n = |
| isPowerOf 2 n = |
||
magicSeries $ floor (logBase 2 (fromIntegral sqr)) |
magicSeries $ floor (logBase 2 (fromIntegral sqr)) |
||
-- where n is not an integer power of 2, we can replicate a |
-- where n is not an integer power of 2, we can replicate a |
||
-- minimum truth table, horizontally and vertically |
-- minimum truth table, horizontally and vertically |
||
| otherwise = |
| otherwise = concat . concat . concat . |
||
scale $ scale <$> |
scale $ scale <$> splitEvery 4 (magicSeries 4) |
||
where |
where |
||
scale = replicate $ quot n 4 |
scale = replicate $ quot n 4 |
||
Line 595: | Line 595: | ||
------------------------------------------------------------------------ |
------------------------------------------------------------------------ |
||
magicSeries :: Int -> [Bool] |
magicSeries :: Int -> [Bool] |
||
magicSeries = (iterate (\xs -> xs ++ (not <$> xs)) [True] !!) |
magicSeries = (iterate (\xs -> xs ++ (not <$> xs)) [True] !!) |
||
Line 601: | Line 601: | ||
splitEvery :: Int -> [a] -> [[a]] |
splitEvery :: Int -> [a] -> [[a]] |
||
splitEvery n = takeWhile (not . null) . unfoldr (Just . splitAt n) |
splitEvery n = takeWhile (not . null) . unfoldr (Just . splitAt n) |
||
isPowerOf :: Int -> Int -> Bool |
isPowerOf :: Int -> Int -> Bool |
||
isPowerOf k n = |
isPowerOf k n = |
||
until (\x -> rem x k /= 0) |
|||
( |
(`quot` k) n == 1 |
||
main :: IO () |
main :: IO () |
||
main = mapM_ print $ magicSquare 8 |
main = mapM_ print $ magicSquare 8 |
||
---------------------------------------------------------------------- |
---------------------------------------------------------------------- |
||
-- Summed and checked |
-- Summed and checked |
||
checked :: Int -> (Int, Bool) |
checked :: Int -> (Int, Bool) |
||
Line 617: | Line 617: | ||
where |
where |
||
square = magicSquare n |
square = magicSquare n |
||
h:t = sum <$> |
h:t = sum <$> square ++ -- rows |
||
transpose square ++ -- cols |
|||
diagonals square -- diagonals |
|||
diagonals :: [[Int]] -> [[Int]] |
diagonals :: [[Int]] -> [[Int]] |
||
diagonals xs = |
diagonals xs = |
||
(\x -> zipWith (!!) x [0..]) <$> [xs, reverse xs] |
(\x -> zipWith (!!) x [0..]) <$> [xs, reverse xs] |
||
main2 :: IO () |
main2 :: IO () |
||
main2 = print $ checked 8</lang> |
main2 = print $ checked 8</lang> |