Instead of looping from 1 to 5000, you compute (1 + 5000) * 5000 / 2
This is what "understand problem first" actually means
sum(n) = is_even(n) ? n/2 * (n+1) : (n+1)/2 * n