(* Project Euler problem 75 *)
let limit = 1500000;;
let rec gcd a b =
if a = b then a
else
if a < b then gcd a (b - a) else gcd (a - b) b;;
let perim m n = (* For use with m and n coprimes with m > n *)
let a = m * m - n * n in
let b = 2 * m * n in
let c = m * m + n * n in
a + b + c ;;
let is_co_prime m n = gcd m n = 1;;
let answer =
let rec aux m acc =
else
let rec aux2 n accu=
if n >= m then aux (m + 1) accu
else
if ((n + m) mod 2 = 1) && is_co_prime m n then
let rec aux3 p accum =
if p > limit then aux2 (n + 1) accum
else
let p2 = 2 * p in
aux3 p2 (p::accum)
in aux3 (perim m n) accu
else aux2 (n + 1) accu
in aux2 1 acc
in aux 2 [];;
let prob75 =
let rec aux lst acc =
match lst with
| [] -> acc
| hd
::tl
when (List.exists
(fun x
-> x
= hd
) tl
) -> aux
(List.filter
(fun x
-> x
<> hd
) tl
) acc
| hd::tl -> aux tl (acc + 1)
in aux answer 0;;
KCogUHJvamVjdCBFdWxlciBwcm9ibGVtIDc1ICopCmxldCBtbGltaXQgPSAoaW50X29mX2Zsb2F0KDc1MDAwMC4gKiogMC41KSk7OwpsZXQgbGltaXQgPSAxNTAwMDAwOzsKCmxldCByZWMgZ2NkIGEgYiA9CiAgaWYgYSA9IGIgdGhlbiBhCiAgZWxzZQogICAgaWYgYSA8IGIgdGhlbiBnY2QgYSAoYiAtIGEpIGVsc2UgZ2NkIChhIC0gYikgYjs7CgpsZXQgcGVyaW0gbSBuID0gICgqIEZvciB1c2Ugd2l0aCBtIGFuZCBuIGNvcHJpbWVzIHdpdGggbSA+IG4gICopCiAgbGV0IGEgPSBtICogbSAtIG4gKiBuIGluCiAgbGV0IGIgPSAyICogbSAqIG4gaW4KICBsZXQgYyA9IG0gKiBtICsgbiAqIG4gaW4KICBhICsgYiArIGMgOzsKCmxldCBpc19jb19wcmltZSBtIG4gPSBnY2QgbSBuID0gMTs7CgpsZXQgYW5zd2VyID0KICBsZXQgcmVjIGF1eCBtIGFjYyA9CiAgICBpZiBtID0gbWxpbW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