diff --git a/DIRECTORY.md b/DIRECTORY.md index 3594d0f..8e53d2b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -49,6 +49,8 @@ * [Problem6](https://github.com/TheAlgorithms/Haskell/blob/master/src/ProjectEuler/Problem6/Problem6.hs) * Problem7 * [Problem7](https://github.com/TheAlgorithms/Haskell/blob/master/src/ProjectEuler/Problem7/Problem7.hs) + * Problem9 + * [Problem9](https://github.com/TheAlgorithms/Haskell/blob/master/src/ProjectEuler/Problem9/Problem9.hs) * Robotics * Complementaryfilter * [Compfilt](https://github.com/TheAlgorithms/Haskell/blob/master/src/Robotics/ComplementaryFilter/CompFilt.hs) diff --git a/src/ProjectEuler/Problem9/Problem9.hs b/src/ProjectEuler/Problem9/Problem9.hs new file mode 100644 index 0000000..02982c6 --- /dev/null +++ b/src/ProjectEuler/Problem9/Problem9.hs @@ -0,0 +1,20 @@ +module ProjectEuler.Problem9.Problem9 where + +-- Generate a list of natural number triples which sum to t +triples :: (Num a, Enum a) => a -> [(a,a,a)] +triples t = [(t-i-j, i, j) | i <- [1..t-1], j<- [1..t-i-1]] + +-- Determine if a triple is pythagorean +isPythagorean :: (Num a, Eq a) => (a,a,a) -> Bool +isPythagorean (a,b,c) = a^2 + b^2 == c^2 + +-- Finds the pythagorean triple such that a + b + c = x; returns a * b *c +solution :: Int -> Maybe Int +solution x = let ts = filter isPythagorean (triples x) + in case ts of + [] -> Nothing + _ -> let (a,b,c) = head ts in Just (a * b * c) + +main :: IO () +main = do + print (solution 1000) \ No newline at end of file