# 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$... – J. M. isn't a mathematician Apr 30 '12 at 13:03

## 5 Answers

I would recommend the following procedure.

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$.

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.

Here is another technique i used to follow. Basically to find the number of odd/even integers in a range, just pick up the first odd/even and last odd/even in that range. Say for example i need to find out the number of odd integers in the range 100 to 150 (inclusive).

1. Pick the first odd integer $\rightarrow$ 101
2. Pick the last odd integer $\rightarrow$ 149
3. Find the difference $\rightarrow$ 149-101= 48
4. Divide by the interval(in this case 2, since the positive difference between any two odd integers is 2). So $\rightarrow$ 48/2 = 24
5. Add 1 for an inclusive range, $\rightarrow$ 24+1=25, This is the step we usually do for all inclusive problems.

This will work for 1-100 like, range(1-99)$\rightarrow$ 99-1=98 $\rightarrow$ 98/2=49 $\rightarrow$ 49+1 = 50.

NOTE: For exclusive set, you should consider if the numbers you pick lie inside the interval in order to consider it as an inclusive set. If its confusing, lets say the above range 100 to 150 is not inclusive, we still pick up numbers 101 and 149 and they lie within the range, so the inclusive property continues here also. But what if i need to find the number of even integers in this range? Same procedure can be used, 1. Pick first and last even integer, 100 and 150 2. 150-100 = 50 3. 50/2 = 25 4. If it is inclusive set, add 1 $\rightarrow$ 25+1=26 5. If it is an exclusive set, subtract 1 $\rightarrow$ 25-1=24

Don't be confused, its just a basic understanding of ranges, read it once more. This is actually very easy and reliable method.

Here is another technique i used to follow. Basically to find the number of odd/even integers in a range, just pick up the first odd/even and last odd/even in that range. Say for example i need to find out the number of odd integers in the range 100 to 150 (inclusive).

1. Pick the first odd integer -> 101
2. Pick the last odd integer -> 149
3. Find the difference -> 149-101= 48
4. Divide by the interval(in this case 2, since the positive difference between any two odd integers is 2). So -> 48/2 = 24
5. Add 1 for an inclusive range, -> 24+1=25, This is the step we usually do for all inclusive problems.

This will work for 1-100 like, range(1-99)-> 99-1=98 -> 98/2=49 -> 49+1 = 50.

NOTE: For exclusive set, you should consider if the numbers you pick lie inside the interval in order to consider it as an inclusive set. If its confusing, lets say the above range 100 to 150 is not inclusive, we still pick up numbers 101 and 149 and they lie within the range, so the inclusive property continues here also. But what if i need to find the number of even integers in this range? Same procedure can be used, 1. Pick first and last even integer, 100 and 150 2. 150-100 = 50 3. 50/2 = 25 4. If it is inclusive set, add 1 -> 25+1=26 5. If it is an exclusive set, subtract 1 -> 25-1=24

Don't be confused, its just a basic understanding of ranges, read it once more. This is actually very easy and reliable method.

This can be used not only for odd/even integers in a range but also for one of the example given above. How many integers of the form 5k+2 are there from 1 to 200?

Starting from k=0, 5(0)+2 = 2 and the last term will be 197, since we need a (multiple of 5)+2 -> 5(39)+2=195+2 The range would look like 2, 7, 12, ... 197 Again our technique, 197-2 = 195 Divide by 5 in this case, since the positive difference is 5 So 195/5=39 Add 1, 39+1=40, Got the answer!!!