Well, how to calculate sum from 1 to 5000 ? Instead of looping from 1 to 5000, you define the relationship instead: sum(1,n) = 1 + n + sum(2, n-1). Isn't this clearer to understand problem first, instead of just looping ?
Well, how to calculate sum from 1 to 5000 ? 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
because of one n and n+1 will always be even. But it show that unfortunately things are often not as straightforward as they first appear.