Skip to content

Commit

Permalink
euler 28 clever
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Korzhuev committed Apr 20, 2012
1 parent 1c483a2 commit 4006b9f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions euler/28.hs
@@ -0,0 +1,25 @@
-- Starting with the number 1 and moving to the right in a clockwise
-- direction a 5 by 5 spiral is formed as follows:
--
-- 21 22 23 24 25
-- 20 7 8 9 10
-- 19 6 1 2 11
-- 18 5 4 3 12
-- 17 16 15 14 13
--
-- What is the sum of the numbers on the diagonals in a 1001 by 1001
-- spiral formed in the same way?

-- It is easy to notice that number in upper-right corner
-- of each square is... odd square
oddSqrs = [x^2 | x <- [1,3..]]

-- We can copute the rest of square numbers like so
divide (x,y) = [y - i*step | i <- [0..3]]
where step = (y - x) `div` 4

sqrNumbers = map divide $ zip oddSqrs (tail oddSqrs)

answer n = succ . sum . concat . take (n `div` 2) $ sqrNumbers

main = prtin $ answer 1001

0 comments on commit 4006b9f

Please sign in to comment.