(* * A number n is a perfect number if the sum of * all its divisors (including itself) equals n *) let fst = fn x => x.1 in let snd = fn x => x.2 in let factorize = fn (x:int): int * int => if x < 2 then (1, x) else (fun g (y) => if y * y > x then (1, x) else if x mod y = 0 then (y, x / y) else g(y+1)) (2) in let sum_of_divisors = fun f(x) => let yz = factorize (x) in let y = fst (yz) in let z = snd (yz) in let aux = fun aux (Y) (z) => if z mod y = 0 then aux (y * Y) (z / y) else ((y * Y - 1) / (y - 1), z) in if y = 1 then 1+z else let Yz = aux (y) (z) in fst(Yz) * f(snd(Yz)) in let perfect = (* find the first perfect number larger than or equal to x *) fun f(x) => if sum_of_divisors (x) = x + x then x else f (x+1) in perfect (1000) (* answer is 8128 *)