mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
Haskell 039
This commit is contained in:
parent
03097ec64d
commit
defbd97d9b
3 changed files with 43 additions and 15 deletions
18
haskell/Util/Triangle.hs
Normal file
18
haskell/Util/Triangle.hs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
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
|
|
@ -9,9 +9,7 @@ Find the product abc.
|
||||||
-}
|
-}
|
||||||
|
|
||||||
import Text.Printf
|
import Text.Printf
|
||||||
|
import Util.Triangle
|
||||||
-- |A triangle consisting of three integral sides
|
|
||||||
data Triangle = Triangle Int Int Int
|
|
||||||
|
|
||||||
-- |Return the product of the sides of the first triangle having the sum of the sides 'sum'
|
-- |Return the product of the sides of the first triangle having the sum of the sides 'sum'
|
||||||
triplet :: Int -> Int
|
triplet :: Int -> Int
|
||||||
|
@ -21,18 +19,6 @@ triplet sum = do
|
||||||
let (Triangle a b c) = tri
|
let (Triangle a b c) = tri
|
||||||
a * b * c
|
a * b * c
|
||||||
|
|
||||||
-- |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
|
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
printf "Pythagorean triplet product having a + b + c = 1000: %d\n" (triplet 1000 :: Int)
|
printf "Pythagorean triplet product having a + b + c = 1000: %d\n" (triplet 1000 :: Int)
|
||||||
|
|
24
haskell/e039.hs
Normal file
24
haskell/e039.hs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
{-
|
||||||
|
|
||||||
|
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
|
||||||
|
|
||||||
|
{20,48,52}, {24,45,51}, {30,40,50}
|
||||||
|
|
||||||
|
For which value of p 1000, is the number of solutions maximised?
|
||||||
|
-}
|
||||||
|
|
||||||
|
import Data.List
|
||||||
|
import Data.Ord
|
||||||
|
import Text.Printf
|
||||||
|
import Util.Triangle
|
||||||
|
|
||||||
|
trimap = do
|
||||||
|
let totals = map (\x -> (x, (length . triangles) x)) [1..999]
|
||||||
|
last (sortBy (comparing snd) totals)
|
||||||
|
|
||||||
|
triangles sum = do
|
||||||
|
let cs = reverse (takeWhile (<= sum - 3) [3..])
|
||||||
|
concat (map (\x -> right_triangles sum x) cs)
|
||||||
|
|
||||||
|
main = do
|
||||||
|
printf "Maximum triangles found with sum = %d\n" (fst trimap)
|
Loading…
Reference in a new issue