3 ms·
But is that how most Julia programmers will write it? I just asked someone in my office and they sent me this: function fact(n::Integer) n < 0 &&
by geocar 9y ago
But is that how most Julia programmers will write it?
I just asked someone in my office and they sent me this:
function fact(n::Integer)
n < 0 && return zero(n)
f = one(n)
for i in 2:n
f *= i
end
return f
end
They seem to have google'd the result, but I'm not judging that: That might be how Julia programmers code...
- improbable22 9y agoDepends whether they cut their teeth on Matlab! I'm not sure which is faster here, often writing out the loop is a little faster but less compact.
- KenoFischer 9y agoThey're equally fast: julia> @btime fact_prod(10000) 7.833 μs (0 allocations: 0 bytes) julia> @btime fact_loop(10000) 7.908 μs (0 allocations: 0 bytes)
- deleted 9y ago[deleted]