-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Korzhuev
committed
Apr 20, 2012
1 parent
1c483a2
commit 4006b9f
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |