Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- A Pythagorean triplet is a set of three natural numbers, a < b < c,
- -- for which, a^2 + b^2 = c^2. For example,
- -- 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean
- -- triplet for which a + b + c = 1000. Find the product abc.
- -- A more efficient solution
- ac :: Integral t => t -> [t]
- ac c =
- [ a * b * c
- | a <- [1 .. div (1000 - c) 2 - 1]
- , let b = (1000 - c) - a
- , a ^ 2 + b ^ 2 == c ^ 2
- ]
- main :: IO ()
- main = print $ head $ concatMap ac [997,996 .. 3]
- -- 31875000
- -- real 0m0,330s
- -- user 0m0,317s
- -- sys 0m0,012s
- -- ################################################################################################
- -- A less efficient solution
- main :: IO ()
- main =
- print $
- head
- [ x * y * z
- | x <- [1 .. 1000]
- , y <- [1 .. 1000]
- , let z = 1000 - (x + y)
- , x + y + z == 1000
- , x ^ 2 + y ^ 2 == z ^ 2
- , x < y && y < z
- ]
- -- 31875000
- -- real 0m0,782s
- -- user 0m0,777s
- -- sys 0m0,004s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement