fork download
  1. (* Project Euler problem 75 *)
  2. let mlimit = (int_of_float(750000. ** 0.5));;
  3. let limit = 1500000;;
  4.  
  5. let rec gcd a b =
  6. if a = b then a
  7. else
  8. if a < b then gcd a (b - a) else gcd (a - b) b;;
  9.  
  10. let perim m n = (* For use with m and n coprimes with m > n *)
  11. let a = m * m - n * n in
  12. let b = 2 * m * n in
  13. let c = m * m + n * n in
  14. a + b + c ;;
  15.  
  16. let is_co_prime m n = gcd m n = 1;;
  17.  
  18. let answer =
  19. let rec aux m acc =
  20. if m = mlimit then (List.sort compare acc)
  21. else
  22. let rec aux2 n accu=
  23. if n >= m then aux (m + 1) accu
  24. else
  25. if ((n + m) mod 2 = 1) && is_co_prime m n then
  26. let rec aux3 p accum =
  27. if p > limit then aux2 (n + 1) accum
  28. else
  29. let p2 = 2 * p in
  30. aux3 p2 (p::accum)
  31. in aux3 (perim m n) accu
  32. else aux2 (n + 1) accu
  33. in aux2 1 acc
  34. in aux 2 [];;
  35.  
  36. let prob75 =
  37. let rec aux lst acc =
  38. match lst with
  39. | [] -> acc
  40. | hd::tl when (List.exists (fun x -> x = hd) tl) -> aux (List.filter (fun x -> x <> hd) tl) acc
  41. | hd::tl -> aux tl (acc + 1)
  42. in aux answer 0;;
  43.  
Time limit exceeded #stdin #stdout 5s 17312KB
stdin
Standard input is empty
stdout
Standard output is empty