Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions src/ProjectEuler/Problem9/Problem9.hs
Original file line number Diff line number Diff line change
@@ -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)