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 count_perfect limit =
  let c = ref 0 in
  for n = 2 to limit do
    if div_sum n = n then c := !c + 1
  done;
  !c

;;

count_perfect 500
