Skip to content

Amicable pairs

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
use Ghul.Pipes
let limit = 20000
sum_of_proper_divisors(n: int) -> int =>
(1::(n / 2))
|> filter(divisor => n % divisor == 0)
|> reduce(0, (total, divisor) => total + divisor)
(1::limit)
|> map(n => (n, partner = sum_of_proper_divisors(n)))
|> filter(((n, partner)) =>
partner > n /\ sum_of_proper_divisors(partner) == n)
|> each(((n, partner)) => write_line("{n} and {partner}"))