Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Find the difference between the sum of the squares of the first one
- -- hundred natural numbers and the square of the sum.
- -- Solution #1
- main :: IO ()
- main = print $ sum [1 .. 100] ^ 2 - sum [x ^ 2 | x <- [1 .. 100]]
- -- 25164150
- -- real 0m0,003s
- -- user 0m0,000s
- -- sys 0m0,003s
- -- Solution #2
- main :: IO ()
- main = print $ foldl (+) 0 [1 .. 100] ^ 2 - foldl (+) 0 (map (^ 2) [1 .. 100])
- -- 25164150
- -- real 0m0,007s
- -- user 0m0,003s
- -- sys 0m0,001s
- -- Solution #3
- check :: (Num a, Ord a) => a -> a -> a -> a
- check d s t =
- if d > 0
- then pow (d - 1) d t
- else t
- pow :: (Num a, Ord a) => a -> a -> a -> a
- pow d s = accum d (s ^ 2)
- accum :: (Num a, Ord a) => a -> a -> a -> a
- accum d s t = check d s (t + s)
- sumRange :: (Ord t, Num t) => t -> t -> t
- sumRange d t =
- if d > 0
- then sumRange (d - 1) (t + d)
- else t
- main :: IO ()
- main = print $ sumRange 100 0 ^ 2 - check 100 0 0
- -- 25164150
- -- real 0m0,007s
- -- user 0m0,003s
- -- sys 0m0,001s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement