42 lines
927 B
Haskell
42 lines
927 B
Haskell
{-
|
|
The sum of the squares of the first ten natural numbers is,
|
|
1^2 + 2^2 + ... + 10^2 = 385
|
|
|
|
The square of the sum of the first ten natural numbers is,
|
|
(1 + 2 + ... + 10)^2 = 55^2 = 3025
|
|
|
|
Hence the difference between the sum of the squares
|
|
of the first ten natural numbers and the square
|
|
of the sum is 3025 - 385 = 2640
|
|
|
|
Find the difference between the sum of the squares
|
|
of the first one hundred natural numbers
|
|
and the square of the sum.
|
|
-}
|
|
upperRange :: Integer
|
|
upperRange = 1_000_000
|
|
|
|
square :: Num a => a -> a
|
|
square n = n^2
|
|
|
|
squares :: [Integer]
|
|
squares = map square [1..upperRange]
|
|
|
|
sum' :: [Integer] -> Integer
|
|
sum' = go 0
|
|
where
|
|
go acc [] = acc
|
|
go acc (x:xs) = go (acc+x) xs
|
|
|
|
sumOfSquares :: Integer
|
|
sumOfSquares = sum squares
|
|
|
|
squareOfTheSum :: Integer
|
|
squareOfTheSum = sum [1..upperRange] ^2
|
|
|
|
solution :: Integer
|
|
solution = squareOfTheSum - sumOfSquares
|
|
|
|
main :: IO ()
|
|
main = print solution
|