SMLにも移植。爆速になったけど、MLtonでもSBCLより遅い。。。
10桁で0.79秒、20桁で9時間56分。
./110 10 0.79s user 0.00s system 99% cpu 0.795 total
./110 20 35746.36s user 4.25s system 99% cpu 9:56:05.18 total
val p = [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71
] : IntInf.int list
fun basis n = (rev o map (fn x => IntInf.pow (10, x)) o List.tabulate) (n, fn x => x)
fun meertens n = let
val lst = List.tabulate (n, fn x => x + 1)
open IntInf
fun loop _ [] _ prod sum _ =
if prod = sum then print (toString sum ^ "\n") else ()
| loop (p::ps) (b::bs) bound prod sum start = let
fun f x = let
val pp = pow (p, x)
in
if pp <= bound then
loop ps bs (bound div pp) (prod * pp) (sum + b * fromInt x) 0
else ()
end
open Int
val lst = List.tabulate (10 - start, fn x => x + start)
in
app f lst
end
in
app (fn i => loop p (basis i) (IntInf.pow (10, i)) 1 0 1) lst
end
val _ = (meertens o valOf o Int.fromString o hd o CommandLine.arguments) ()
omoikani
#4903()
[
StandardML
]
Rating0/0=0.00
val p = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 ] : IntInf.int list fun basis n = (rev o map (fn x => IntInf.pow (10, x)) o List.tabulate) (n, fn x => x) fun meertens n = let val lst = List.tabulate (n, fn x => x + 1) open IntInf fun loop _ [] _ prod sum _ = if prod = sum then print (toString sum ^ "\n") else () | loop (p::ps) (b::bs) bound prod sum start = let fun f x = let val pp = pow (p, x) in if pp <= bound then loop ps bs (bound div pp) (prod * pp) (sum + b * fromInt x) 0 else () end open Int val lst = List.tabulate (10 - start, fn x => x + start) in app f lst end in app (fn i => loop p (basis i) (IntInf.pow (10, i)) 1 0 1) lst end val _ = (meertens o valOf o Int.fromString o hd o CommandLine.arguments) ()Rating0/0=0.00-0+