Skip to content
Browse files

euler 28 clever

  • Loading branch information...
1 parent 1c483a2 commit 4006b9f6ce812021fad820c6b7c3bd4a1423ad76 @andrusha committed Apr 20, 2012
Showing with 25 additions and 0 deletions.
  1. +25 −0 euler/28.hs
View
25 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.
Something went wrong with that request. Please try again.