Formula for the sum of even/odd numbers in a consecutive set?

viktr1

New member
Joined
Nov 5, 2019
Messages
1
So we all heard of the story of Gauss and the sum(1 to 100); whereas the equation for figuring out the sum of any consecutive integer is sum = (n/2)(first + last); whereas if the first number is a non-zero integer the n = (last - first) + 1

For example:

The sum of consecutive numbers from an arbitrary 237 to an arbitrary 796 can be easily found by
1. n = (796 - 237) + 1 = 560

2. (560/2)(237+796) = 289,240.

However, how would this formula be expressed mathematically if we are trying to find the sum of all the even/odd numbers in a consecutive set?

1. The first challenge is figuring out "nodd" or "neven"
For example, a number set [4 to 9] includes 3 evens [4, 6, 8] and 3 odds [5, 7, 9]. This means "nodd" can be expressed as n/2, or in other words

nodd = ((last - first) + 1) / 2.

However, if the set of numbers is [4 to 8], then it includes 3 evens [ 4, 6, 8] and 2 odds [5, 7] then this equation fails, as n = 5 but nodd = 2, and 2 =/= 3.

So how do we express this mathematically, so that an arbitrary range of numbers can easily be counted?
 
The sum of all even integers, from \(\displaystyle 2n_1\) to \(\displaystyle 2n_2\), that is, \(\displaystyle 2n_1+ (2n_1+ 2)+ (2n_1+ 4)+ \cdot\cdot\cdot+ 2n_2\), can be written as \(\displaystyle 2(n_1+ (n_1+ 1)+ (n_1+ 2)+ \cdot\cdot\cdot+ n_2)\) or 2 times the sum of all integers from \(\displaystyle n_1\) to \(\displaystyle n_2\). Using your formula, the sum of all integers from \(\displaystyle n_1\) to \(\displaystyle n_2\) is \(\displaystyle (n/2(n_1+ n_2)\) with \(\displaystyle n= n_2- n_1+ 1\), the sum of all even integers from \(\displaystyle 2n_1\) to \(\displaystyle 2n_2\) is \(\displaystyle n(n_2+ n_2\). For example, the sum of all even integers from 4= 2(2) to 10= 2(5) is 4(2+ 5)= 4(7)= 28. Check: 4+ 6+ 8+ 10= 10+ 18= 28.

To get the sum of all odd integers, from \(\displaystyle 2n_1+ 1\) to \(\displaystyle 2n_2+ 1\), find the sum of all integers from \(\displaystyle 2n_1+ 1\) to \(\displaystyle 2n_2+ 1\) and subtract the sum of even integers between them. For example, to find the sum of all odd integers from 3 to 9, we find the sum of all integers from 3 to 9. From your formula that is 7(3+ 9)/2= 7(12)/2= 7(6)= 42 (check: 3+ 4+ 5+ 6+ 7+ 8+ 9= 7+ 11+ 15+ 9= 18+ 24= 42) while the sum of all even integers from 4= 2(2) to 8= 2(4) is 3(2+ 4)= 3(6)= 18 (check: 4+ 6+ 8= 10+ 8= 18). So the sum of all odd integers from 3 to 9 is 42- 18= 24 (check: 3+ 5+ 7+ 9= 8+ 16= 24).
 
1. The first challenge is figuring out "nodd" or "neven"
For example, a number set [4 to 9] includes 3 evens [4, 6, 8] and 3 odds [5, 7, 9]. This means "nodd" can be expressed as n/2, or in other words

nodd = ((last - first) + 1) / 2.

However, if the set of numbers is [4 to 8], then it includes 3 evens [ 4, 6, 8] and 2 odds [5, 7] then this equation fails, as n = 5 but nodd = 2, and 2 =/= 3.

So how do we express this mathematically, so that an arbitrary range of numbers can easily be counted?
You should be able to count the number of even numbers in [m, n] as a piecewise-defined function (that is, combining several separate cases into one function), and that could likely be rewritten in terms of the floor or ceiling functions, if you considered that easier to evaluate.

To start, what four cases are there? You've dealt with the case where m is even and n is odd.
 
Top