# How do I determine the number of odd integers in a range?

Say I have a range of integers, [1..100]. How do I determine the number of odd integers in that range? Does it make a difference if the beginning and end numbers are [odd .. odd], [even .. odd], [even .. even]?

-
To simplify things: consider first the case where you start from $1$, or $2$... –  Ｊ. Ｍ. Apr 30 '12 at 13:03

Hint: Often it helps to try small enough ranges that you can just count them, like [1,4], [1,5],[2,5],[2,6]. Whether it makes a difference whether the ends are odd or even depends upon the expression you come up with. One expression is to say that half the numbers in your range are odd, then correct for the end effects-adding an odd number to the list increases the number of odds by 1, but half the total number by only 1/2, so you will have to add the other 1/2 in.

-
• Look at the top of the range: if it is odd then add 1, if even leave alone.

• Look at the bottom of the range: if it is odd then subtract 1, if even leave alone.

• Take the difference between the new even top and bottom; then divide by two.

So for the range $[1,100]$ you get $\frac{100-0}{2}=50$ odd integers.

-
The number of odds from $1$ to $100$ is the same as the number of evens from $2$ to $101$ which is the same as the number of evens from $2$ to $100$ which is the same as the number of integers from $1$ to $50$. This is $50$.
With a bit of practice this will work flawlessly, and you will retain full control over the logic. It also generalizes nicely. How many numbers of the form $5k+2$ are there from $1$ to $200$? There are just as many as there are numbers divisible by $5$ from $4$ to $203$, which is the same as the number of numbers divisible by $5$ from $5$ to $200$, which is the same as the number of integers from $1$ to $40$, which is $40$.