-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem39.fsx
36 lines (27 loc) · 865 Bytes
/
problem39.fsx
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
29
30
31
32
33
34
35
36
(* Project Euler Problem 39
* By Weisi Dai <[email protected]>
*)
let ubound = 1000
let triangleInequality a b c =
(c < a + b) && (c > b - a)
let formsTriangle a b c =
(triangleInequality a b c)
&& (triangleInequality b c a)
&& (triangleInequality c a b)
let formsRightTriangle a b c = // a <= b <= c
(formsTriangle a b c)
&& (pown a 2 + pown b 2 - pown c 2 = 0)
let rightTrianglesOfSum sum = seq {
for a in 1 .. sum / 3 do
for b in a .. (sum - a) / 2 do
let c = sum - a - b
if formsRightTriangle a b c then
yield (a, b, c)
}
let rightTrangles = seq {
for sum in 3 .. ubound do
yield (sum, Seq.length (rightTrianglesOfSum sum))
}
let problem39 = rightTrangles |> Seq.maxBy snd |> fst
let main() = printfn "The answer is %d." (problem39)
main()