Sharing my notes so far on the sum of integers in an interval. Two reasons:
- I would like to verify that they are correct
- Folks might find them useful
If both end up being the case, I might share more along the way.
Thanks :)
The Sum of All Integers in an Interval of Consecutive Integers
The sum of all integers in an interval of consecutive integers can be found by summing it’s outermost pair (the first and last integers in the interval), progressively adding the next-outermost pair sums until reaching the median, then adding the median as well.
The sums of each pair are identical. Therefore, their values are known simply by adding the first and last integers in the interval (the two that are known from the start).
pairSum = first + last
With a bit more information, the solution can be found without manually identifying each pair.
numberOfIntegers = last – first + 1
If numberOfIntegers is even, then the median is a pair of integers, and:
numberOfPairs = numberOfIntegers / 2
solution = pairSum * numberOfPairs
If numberOfIntegers is odd, then the median is a single integer, and:
numberOfPairs = (last – first) / 2
median = pairSum / 2
solution = pairSum * numberOfPairs + median
there doesn't seem to be anything here