Ok, so to clear things up here, you could in principle port the Mathematica 
solution to Julia, and it would very likely be faster.

divsum(n) = DivisorSigma(1, n) - n
isamicable(n::Integer) = divsum(n) != n && divsum(divsum(n)) == n
@time sum(filter(isamicable, 1:9999))

The only problem is that we don't have a carefully optimised DivisorSigma 
function in the standard library. That's it; there's no magic going on 
here, in Julia or Mathematica. But what you're comparing is not Julia and 
Mathematica, it's a naive algorithm in Julia vs. a different algorithm in 
C. Now, fair enough, this perhaps says something about Mathematica's 
impressive standard library compared to Julia's, but it really doesn't make 
any sense as a performance comparison.

If you only ever need to rely on built-in functions and write a few lines 
of code for yourself, Mathematica is great. But Julia's real benefit comes 
when the standard library lets you down. You can simulate this, as Jason 
points out, by forgetting that DivisorSigma[] exists; so let's try solving 
the problem again, with the same algorithm you used in Julia originally in 
both languages:

divsum(n) = sum(map(x-> n%x == 0, 1:n-1))
isamicable(n::Integer) = divsum(n) != n && divsum(divsum(n)) == n
@time sum(filter(isamicable, 2:9999))

DivSum[n_] := Total[Select[Range[n - 1], Mod[n, #] == 0 &]]
AmicableQ[n_Integer] := DivSum[n] != n && DivSum[DivSum[n]] == n
Plus @@ Select[Range[9999], AmicableQ] // Timing

The Julia above takes 18 seconds on my system, whereas the Mathematica 
takes 160 seconds. Mathematica is slow when you step outside the standard 
library.

On the other hand, as others have shown, a really fast implementation was 
achievable in Julia despite the fact that it wasn't built in - that's what 
you should take away from this.

Reply via email to