let rec gcd a b = if b = 0 then a else gcd b (a mod b)

let count_primitive_triples n =
  let c = ref 0 in
  for m = 2 to 50 do
    let kk = ref 1 in
    while !kk < m do
      if (m - !kk) mod 2 = 1 && gcd m !kk = 1 then begin
        let h = m * m + !kk * !kk in
        if h <= n then c := !c + 1
      end;
      kk := !kk + 1
    done
  done;
  !c

;;

count_primitive_triples 100
