-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_009.hs
28 lines (23 loc) · 867 Bytes
/
problem_009.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{-
Using Euclid's formula to generate Pythagorean triplets:
https://en.wikipedia.org/wiki/Pythagorean_triple
Solution takes advantage of lazy evaluation, and that's why an infinite
list of pairs of values (m, n) (following naming convention of Euclid's
formula) is generated.
-}
problem9 :: Int
problem9 =
let targetSum = 1000
pairs = [(m, n) | m <- [2..], n <- [1..m - 1]]
condition triplet = sumTriplet triplet /= targetSum
in productTriplet $ head $ dropWhile condition $ map pythagoreanTriplet pairs
pythagoreanTriplet :: (Integral a) => (a, a) -> (a, a, a)
pythagoreanTriplet (m, n) =
let a = m^2 - n^2
b = 2 * m * n;
c = m^2 + n^2;
in (a, b, c)
productTriplet :: (Integral a) => (a, a, a) -> a
productTriplet (a, b, c) = a * b * c
sumTriplet :: (Integral a) => (a, a, a) -> a
sumTriplet (a, b, c) = a + b + c