Jump to content

Magic squares of doubly even order: Difference between revisions

Line 279:
16 15 51 52 53 54 10 9
8 7 59 60 61 62 2 1</pre>
 
=={{header|Haskell}}==
 
<lang Haskell>magicSquare :: Int -> [[Int]]
magicSquare n
| (rem n 4) > 0 = []
| otherwise = splitEvery n $
map (\(x, i) -> if x then i else limit - i) series
where
scale = replicate $ quot n 4
sqr = n * n
limit = sqr + 1
series = zip
(concat $ concat $ concat $
scale $ map scale $ splitEvery 4 $ magicSeries 5)
[1..sqr]
 
magicSeries :: Int -> [Bool]
magicSeries n
| n <= 1 = [True]
| otherwise = xs ++ map not xs
where
xs = magicSeries (n - 1)
splitEvery :: Int -> [a] -> [[a]]
splitEvery n xs
| length xs <= n = [xs]
| otherwise = [gp] ++ splitEvery n t
where
(gp, t) = splitAt n xs
 
main :: IO ()
main = mapM_ (putStrLn . show) $ magicSquare 8</lang>
 
{{Out}}
<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]
[25,39,38,28,29,35,34,32]
[33,31,30,36,37,27,26,40]
[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>
 
=={{header|Java}}==
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.