RosettaCodeData/Task/Amicable-pairs/ATS/amicable-pairs.ats
2023-07-01 13:44:08 -04:00

104 lines
1.4 KiB
Text

(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
#include
"share/HATS/atspre_staload_libats_ML.hats"
//
(* ****** ****** *)
//
fun
sum_list_vt
(xs: List_vt(int)): int =
(
case+ xs of
| ~list_vt_nil() => 0
| ~list_vt_cons(x, xs) => x + sum_list_vt(xs)
)
//
(* ****** ****** *)
fun
propDivs
(
x0: int
) : List0_vt(int) =
loop(x0, 2, list_vt_sing(1)) where
{
//
fun
loop
(
x0: int, i: int, res: List0_vt(int)
) : List0_vt(int) =
(
if
(i * i) > x0
then list_vt_reverse(res)
else
(
if x0 % i != 0
then
loop(x0, i+1, res)
// end of [then]
else let
val res =
cons_vt(i, res)
// end of [val]
val res =
(
if i * i = x0 then res else cons_vt(x0 / i, res)
) : List0_vt(int) // end of [val]
in
loop(x0, i+1, res)
end // end of [else]
// end of [if]
)
) (* end of [loop] *)
//
} // end of [propDivs]
(* ****** ****** *)
fun
sum_propDivs(x: int): int = sum_list_vt(propDivs(x))
(* ****** ****** *)
val
theNat2 = auxmain(2) where
{
fun
auxmain
(
n: int
) : stream_vt(int) = $ldelay(stream_vt_cons(n, auxmain(n+1)))
}
(* ****** ****** *)
//
val
theAmicable =
(
stream_vt_takeLte(theNat2, 20000)
).filter()
(
lam x =>
let
val x2 = sum_propDivs(x)
in x < x2 && x = sum_propDivs(x2) end
)
//
(* ****** ****** *)
val () =
theAmicable.foreach()
(
lam x => println! ("(", x, ", ", sum_propDivs(x), ")")
)
(* ****** ****** *)
implement main0 () = ()
(* ****** ****** *)