-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp39.hs
28 lines (22 loc) · 862 Bytes
/
p39.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Data.List
--given a and p, c^2 - b^2 = a^2 and c + b = p - a
-- b = sqrt(c^2 - a^2), c + sqrt(c^2 - a^2) = p - a
-- c^2 - a^2 = (p - a - c)^2
-- c^2 - a^2 = p^2 - pa - pc - ap + a^2 + ac - cp + ac + c^2
-- -2 * a^2 = p^2 - 2 * pa - 2 * pc + 2 * ac
-- 2 * pa - 2 * a^2 - p^2 = 2 * c * (a - p)
-- c = (2 * (pa - a^2) - p^2) / (a - p)
evalC :: Int -> Int -> Int
evalC a p = div (2 * (p * a - a^2) - p^2) (2 * (a - p))
isRightTriangle :: Int -> Int -> Bool
isRightTriangle a p =
let c = evalC a p
b = floor $ sqrt $ fromIntegral (c^2 - a^2)
in a + b + c == p && a^2 + b^2 == c^2
solutionCount :: Int -> Int
solutionCount p = div (length $ filter ((flip isRightTriangle) p) [1..(div p 2 - 1)]) 2
p39 :: Int
p39 =
let allSols = map solutionCount [0..1000]
maxVal = maximum allSols
in maybe 0 id $ elemIndex maxVal allSols