2014년 5월 5일 월요일

Euler Project: Problem 28

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
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

main :: IO()
main = print $ 1 + sumOf2ndTo1001Diag 
sumOf2ndTo1001Diag :: Int
sumOf2ndTo1001Diag = sum $ map sumCurrDiag inputData 
--초항: 예를 들어 3x3은, prev인 1x1 + (3-1).
iniNum :: (Int, Int) -> Int
iniNum (prev, curr) = prev * prev + (curr - 1) 
--3x3에서는 iniNum에서 (3-1)의 등차수열.
sumCurrDiag :: (Int, Int) -> Int
sumCurrDiag (prev, curr) = 2 * ( 2 * iniNum (prev, curr) + 3 * (curr - 1))
--sumCurrDiag (prev, curr) = iniNum (prev, curr) * 4 + (curr - 1) * 6 
inputData :: [(Int, Int)]
inputData = zip baseData (tail baseData) 
baseData :: [Int]
baseData = [1,3..1001] 
*Main> main
669171001

댓글 없음:

댓글 쓰기