total = 0
for i in range(0, some upper bound):
total += i
Sorry if this is basic but I have a lot of these and they\'re taking up more room than is
total = some_upper_bound * (some_upper_bound -1) / 2
if lower_bound != 0
:
total = (some_upper_bound - lower_bound) * (some_upper_bound + lower_bound - 1) / 2
Update: I would've deleted my answer as it is practically an exact copy of part of the accepted answer (although I answered independently). There is, however, one - very small, but theoretically interesting improvement when lower_bound
is involved: my answer contains only two multiplications / divisions (which are relatively more expensive than additions/subtractions) while the other answer contains four.