The funny thing is that I didn’t think of digits at first and instead used string and apparently its way faster than using the built in digits function, as this allocates. Inspecting this I found that
-
stringuses internally gmp to get nice speed on BigInt: https://github.com/JuliaLang/julia/blob/c94e8d2a387a9e7899606ffe740fd44d6106b6fb/base/gmp.jl#L682 -
whereas
digitsisn’t specialized onBigIntand uses a lot of allocating and slowdivremcalls.
julia> euler20(n) = mapreduce(x->Int(x)-48,+,string(factorial(big(n))))
euler20 (generic function with 1 method)
julia> @btime euler20(100)
1.190 μs (13 allocations: 544 bytes)
648