mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
19 lines
663 B
Haskell
19 lines
663 B
Haskell
|
module Util.Triangle where
|
||
|
|
||
|
-- |A triangle consisting of three integral sides
|
||
|
data Triangle = Triangle Int Int Int
|
||
|
deriving(Show, Eq)
|
||
|
|
||
|
-- |Find all right triangles having sum of sides 'sum' and side c length of 'c'
|
||
|
right_triangles :: Int -> Int -> [Triangle]
|
||
|
right_triangles sum c = do
|
||
|
let diff = sum - c
|
||
|
let range = [1..(floor ((fromIntegral diff) / 2)) + 1]
|
||
|
let triangles = filter is_right_triangle (map (\x -> Triangle x (diff - x) c) range)
|
||
|
triangles
|
||
|
|
||
|
-- |Return whether the provided triangle is a right triangle using the pythagorean theorem
|
||
|
is_right_triangle :: Triangle -> Bool
|
||
|
is_right_triangle (Triangle a b c) =
|
||
|
a^2 + b^2 == c^2
|