Project Euler Problem 75 Solution
It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.
12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.
120 cm: (30,40,50), (20,48,52), (24,45,51)
Given that is the length of the wire, for how many values of can exactly one integer sided right angle triangle be formed?
import Data.Array triples :: Int -> [Int] triples limit = [a + b + c | u <- [1..l'], v <- [u+1,u+3..l'-u], gcd u v == 1, let a = v^2 - u^2, let b = 2*u*v, let c = u^2 + v^2] where l' = round $ sqrt $ fromIntegral limit perimeters :: Int -> Array Int Int perimeters limit = accumArray (+) 0 (1, limit) $ map (\p -> (p, 1)) $ concat [takeWhile (<= limit) $ map (*p) [1..] | p <- triples limit] main :: IO () main = print $ length $ filter (== 1) $ elems $ perimeters 1500000
$ ghc -O2 -o singular-triangles singular-triangles.hs $ time ./singular-triangles real 0m0.844s user 0m0.828s sys 0m0.008s
Questions? Comments? Send me an email: firstname.lastname@example.org