As an example I take a classic algorithm scripting challenge (aka Sum All Numbers in a Range): Create a function which returns the sum of all consecutive integers between two values (passed as an array).
My immediate approach to this was to recognise that there’s an extremely simple algebraic formula which takes care of this for any two values (a, b, where a>b):
(To mitigate against cases where b>a you may include math.abs).
Essentially, the function can be reduced to a single, simple line of code.
But does this mean that such a solution is inherently more efficient or better performing than another which may take a more circuitous route?
Four solutions are provided in the FCC guide to the challenge, all of which depend on recursion, for loops or slightly cumbersome counting mechanisms.
Are these solutions better or faster than the one I found (and if so, how would I know)?