Certainly nothing wrong with counting them all if you don't know a quicker way - better that than doing nothing! Still, you might run into a set with a few hundred elements, and then counting wouldn't be an option.
Let's look at a problem where it would be impractical to count one by one: how many multiples of 3 are there between 100 and 1000?
There are a few ways to do this. You could, for example, do as follows:
-Figure out where your set starts and ends. It's easy to recognize multiples of 3 (the sum of the digits is divisible by 3), so we want to know how many numbers are in this list:
102, 105, 108, 111, ..., 990, 993, 996, 999
-Any set of consecutive multiples is what I'll call 'evenly spaced' - here, we go up by 3 each time to find the next number in the list. We can always count how many elements are in an evenly spaced list:
- Figure out how far you go from the start of the list to the end, so find: largest - smallest;
- Now, we're only looking at every third number, so we want to divide by 3 (the 'space' between each number in the list)
- Finally, we need to add 1 at the end, or else we're leaving out the smallest number.
That is, in any 'evenly spaced' list, you can find the number of elements by calculating:
[(largest - smallest)/'space' ] + 1
Here we get:
[(999 - 102)/3] + 1 = 299 + 1 = 300
'Evenly spaced' sets are very common in mathematics - consecutive integers, consecutive even or odd integers, consecutive multiples of any number, and any list of consecutive numbers that give a fixed remainder when dividing by something are all evenly spaced lists. So it can be useful to know about them. Not only can you count the number of elements in an evenly spaced list using the above, but it's also very useful to know that in an evenly spaced set, the average, the median, and the average of the largest and smallest elements are always equal to each other.
There are often simpler approaches to these kinds of problems - the formula above is never actually necessary. Here, we could have factored out a 3 from each number in the list:
102, 105, 108, 111, ..., 990, 993, 996, 999
= 3*34, 3*35, 3*36, 3*37, ..., 3*330, 3*331, 3*332, 3*333
from which we can see that the list 34, 35, 36, 37, ..., 330, 331, 332, 333 is the same length as the list 102, 105, ..., 996, 999. Now subtracting 33 from each number, we'll get a set of the same size:
1, 2, 3, 4, ..., 297, 298, 299, 300
which clearly has 300 elements.