let div_sum n =
  let s = ref 1 in
  let i = ref 2 in
  while !i * !i <= n do
    if n mod !i = 0 then begin
      s := !s + !i;
      let q = n / !i in
      if q <> !i then s := !s + q
    end;
    i := !i + 1
  done;
  if n = 1 then 0 else !s

let euler21 limit =
  let total = ref 0 in
  for a = 2 to limit do
    let b = div_sum a in
    if b <> a && b > a && b <= limit && div_sum b = a then
      total := !total + a + b
  done;
  !total

;;

euler21 300
