Counting isn’t easy. Suppose your boss wants you to work from 8am to 11am, and mop floors 8 to 11. Simple – it’s one floor per hour, right?

Nope! There are 4 floors to mop (8, 9, 10 and 11) but only 3 hours to work (8-9, 9-10, and 10-11).

Whoa — we count floors and hours differently? You bet. And somehow, if the boss said “Mop floors 8 to 11 on April 8th to 11th” everything would be ok.

Today let’s unravel this counting mystery.

## Basic Counting

Numbers help us know “how many”. Take a numbered list of houses:

There’s 52 houses there. Don’t count each one: the addresses count for us! The numbers label the houses one by one, just as we’d do. We can just read the last item: “1 to 52″ is 52 houses.

**Examples:**

- Employee ID cards from 1 to 3493. There are 3493 employees.
- Days of the month from 1 to 31. There are 31 days in that month.

## What Does Subtraction Mean?

We often see numbers as points on a line:

These points can be houses, floors, or plain integers. Whatever they are, they’re labeled so we can count them easily. 1 to 10 means ten items.

But what about a range like 8 to 11? How many items are there?

We’d probably try subtraction: 11 – 8 = 3, right? But here’s the key:

**Subtraction is a span between numbers, not a count**

The equation 11 – 8 = 3 means there are 3 “spans” between 8 and 11, but four numbers in that range!

A span is a *distance measure*, like time from 8am and 11am (3 hours) or the distance between 8 and 11 inches (3 inches).

But when counting floors, we aren’t asking for the distance between floors 8 to 11 (which is in fact 3 floors or 30 feet, assuming 10 feet per floor). We want a count of how many items the range “8 to 11″ includes!

## Working With Spans and Counts

Realizing there are **two types of counting** was a big mental shift. We have two possible choices when “counting” from a to b:

**Distance from a to b:**b – a [regular subtraction]**Number of items from a to b:**b – a + 1 (span touches extra element)

Ok, the formulas work. But why does a count need an extra element?

Well, the shortest span (distance 1) actually touches two numbers:

A span is a line segment with a start and end; a span of distance 1 covers 2 points. As we grow the span, we gobble up more points and are always “one ahead”.

Here’s another way to think about it. A span of 3 means we start with an item (#8) and count out 3 more (#9, #10, #11). So, a span of N includes 1 original item and N new items, for a total of N + 1 items.

I like seeing new viewpoints; use what works for you.

## The Fencepost Problem

The confusion between spans and counts is commonly called the fencepost problem. Are you counting the posts (points) or the distance between them (fence spans)?

The question goes like this:

You’re building a fence 100 feet long, with posts every 10 feet. How many posts do you need?

Here’s how to think about it with our new mental model:

Hrm, we want a fence 100 feet long. Ok: that’s a span of ten, 10-foot segments. But we want the number ofposts: how many posts do those ten segments touch? Well, a span always touches an extra point, so ten segments means 11 posts.

11 fenceposts it is. But the problem isn’t natural for me – I have to think about spans vs points. I’d double-check with a smaller example – a fence of length 10 would have 2 posts, so yes, we need an “extra post”.

A few more examples:

**Working Days:**I worked April 8th to April 11th. How many days did I work? Well, that’s a span of 3 (11-8), but we “touch” 4 days: April 8, 9, 10, 11. So I worked 4 days.**Hours:**Hours are like spans. Working from 8 to 11 means you are covering the*spans*8-9, 9-10, and 10-11. 8 to 11 means a “time” of 3 hours.**Seconds:**I start a race, and the start time at 12:01:08 (12 hours, 1 minute, 8 seconds). It ends at 12:01:11. How long did it go? 11 – 8 = 3 seconds. (Short race)

Interesting, eh? Some units of time are measured with spans (seconds) and others are items to be counted (days).

The measuring type depends on the context. We see small units of time as “instants” and want the duration between those instants, not the “number” of instants we touched.

We see days as a large fuzzy blob covering a time period (9am-5am) — and we want to know how many blobs we covered. Saying you worked April 8 to April 9th implies you worked a timespan of 9am-5pm on two days.

The counting type depends on the context – but at least you know *why* we count them differently.

## Final Thoughts

Counting isn’t simple. The fencepost problem and other “off by one” boundary errors are notoriously common. But don’t just remember a special trick (add 1) – remember there are two ways to measure something:

**Are we measuring a difference (distance, time)?**Then do regular subtraction to get a span.**Are we counting items?**Then subtract and add 1 (b – a + 1) – we want the number of items the span touches.

It’s not easy to recognize the difference – we’re used to having one way to measure. Try a simple example (a fence 10 feet long) to test if you’ve got a count or span.

I’ve run into the fencepost problem many times, but the articles I read just told me how to fix it. No, no, no – *why does it happen*?

It turns out we often use the same approach for two different types of counting. And while the right method may not be obvious, at least we know to try both approaches. Happy math.

Your article prints out several question marks and symbols instead of the characters. I’m in the U.S. and it’s displaying that way both in Safari and Firefox.

It looks like a copy-paste from Word.

http://www.atchius.net/betterexplained.png

For your reference… that’s how your most recent blog entry looks in Safari on a Mac. RSS feed in Mail.app is the same. In Camino, I get regular ?s instead of the diamond icons.

Your past entries are fine.

Thanks for the fast response guys! I was using a new publishing process and didn’t realize it had all these issues — fixing them up right now.

Ok, the weird characters should be fixed. Thanks again for the notice everyone!

That’s a great explanation. I really like your articles (this is a problem that always frustrates me, as well).

The mathematical terms for that would be inclusive or exclusive, if anyone remembers “sets” from elementary school (or junior high – it’s been awhile). Inclusive means add the 1 (count the number of items), and exclusive means don’t add it (tell me the distance between).

The difference is caused by the adoption of closed intervals in our language. When we say “all integers between 1 and N”, we mean N numbers (i.e., we include the both the boundaries).

It is often more convenient (at least in programming) to work with half-open intervals. Consider a standard way of cycling through N numbers in C-like languages:

for (int i = 0; i

We mention boundaries 0 and N, yet we only mean N numbers, not N+1.

You can read more about this in Dijkstra’s note: Why numbering should start at zero.

The code snippet in my comment above should read:

for (int i = 0; i < N; i++)

…

Sorry about that.

Interesting post on a common issue. Thanks!

Thanks Marian, that link was interesting too. It was an excellent

argument, but I didn’t understand the bit around what follows.

‘Latter’ refers to examples (a) and (c). I don’t follow why inclusion

of the upper bound would force the upper bound to be unnatural as the

set shrinks to the empty set. (sorry about the awkward use of the word

bound) Given 2 =< i =< 3 we have {2,3}, given 2 =< i =< 2 we have

only {2}, with 2 =< i < 2 we still have {2} (or nonsense).

Is it because beginning with zero you can’t show the empty set with

approach (c), but you can with the (a)?

The passage: “Consider now the subsequences starting at the smallest

natural number: Inclusion of the upper bound would then force the

latter to be unnatural by the time it had shrunk to the empty one.”

Hey,

Thanks a lot for your article. I never looked at counting numbers like this before.

A very clear and easy explanation of things.

Looking forward to more posts from you.

Thanks for the article. I love when you post new stuff.

Please, keep posting

Great post. I like the idea of thinking in spans and also physical entities. It’s something we all do but maybe have not consciously thought about it.

This reminded me of how the years of the 1800’s are the 19th century (and not the 18th century).

Keep em coming!

@Phil, @ Marian: Thanks for the details! Yes, it’s interesting how certain types of time ranges imply inclusive or exclusive ranges. We tend to “know” which one to use given the context and/or idioms like the for loop (always start at 0, always be less than N).

@Queryman: I think my form may have eaten your comment, I think you may have written “Given 2 = i”

@Scientific Chick: Thanks!

@Ismeet: You’re welcome, appreciate the encouragement

@Tiago: Thanks!

@CL: Ah, that 19th centry / 1800s issue has been on my mind too, great example. The first “span” of a hundred years (century) is from year 1 to 100, leading to all the confusion we now suffer. Will try to keep them coming :).

OMG! That’s how I’ve envisioned counting things/distances/etc for years! I thought I was the only one! I always said “you have to start at the beginning of 5, but you go to the end of 8″ so that makes 4 not 3!

Music is written in such a way. Bar lines are the points for counting measures, so that statements like “four measures back” aren’t ambiguous.

When I began numbering my scores, I noticed that a four measure rest starting at measure 21 encompasses spans 21-22, 22-23, 23-24, and 24-25, but it only numbers 21-24, because the measure between 24 and 25 is numbered 24. That was the epiphany that there are two different counting systems, and that they must coexist.

@Jehan: Nice! It’s really cool figuring out the tricks other people use to handle potentially confusing topics.

@Nobody: Great example with music. I think it makes sense, as music must convey timing accurately and clearly. It’s an interesting solution to just “number the fence pieces” to avoid confusion about what 4 measures back means. Thanks for the example.

this post reminded me of an old puzzle:

If you take one pill every half hour, how many will you take in one hour?

I use this counting on daily hospital rounds. In surgery we keep track of Hospital Day (HD) and Post-Op Day (POD) for our patients. HD is a fencepost count and POD is a measure of the interval from the day of operation.

@andrew: Interesting puzzle — depends on whether you count 1:00, 1:30 and 2:00 as belonging to the same hour

@Brian: Cool, thanks for the real world example!

Also depends on when you take your first pill:

If you take a pill at 1:00, then in the hour 1:01 to 2:01 you will have taken just two pills.

Finally, a clear way explanation of why 1 Jan 2000 was not the beginning of the new century (and millennium) – 1 Jan 2001 was!

I’d like to add one more thing here…

Consider cutting a cake into 10 pieces ?? How many times do you need to cut; it’s 9.

Are you sure? I think I can cut a cake into 10 pieces with just 5 cuts.

Another real-world example:

Suppose you are designing a 10′ high stairway with 6″ high risers and 12″ wide treads (a ratio of 1:2). What is the length of the stair? You might think it would be 20′ (twice the height).

But look:

The stair will have a total of 20 risers (20 x 6″ = 120″ = 10′-0″). However, the stair only has 19 treads, so the total length of the stair will be 19′ (not 20′)!

When measuring the height of the stair, the risers are “spans.” When considering the length of the stairway, the risers become the “fence posts”, with the treads being the spans in between.

I’m an architect, and I still have to make myself a little diagram to figure this out on each new project!

Mikekoz-

I think you can also cut a cake into 11 pieces with just 4 cuts. These would be strange pieces, of course!

Hi Kalid,

just wanted to say that I have been following your posts through RSS for a while now and find them really useful. You have an excellent way of communicating the essence of an idea/concept distilled to its base units. This explanation on counting is something that many people notice, but few can annunciate well. Thanks.

@Alexander: Thanks for dropping by and for the comment. I really want to break things down to their simplest elements, so I’m glad it’s coming through! I think most concepts have a simple essence, but often times we try to jazz it up to make it seem more “impressive” :).

Great post, as usual.

This is related to the puzzle:

On a typical Wednesday, how many times does the minute hand pass the hour hand?

The answer is probably not the first thing that comes to mind–or the second. This puzzle takes the fencepost problem to a new level.

Thanks for this amazing article! My kids and I have noticed this come up in the past and we didn’t know there was a name for it. I’m going to put it up on the whiteboard in out family room and I’m sure they’ll be fascinated by it.

@Rebecca: Awesome! Yes, sometimes even just having a name for something makes you aware of it (when I’m counting things, I always double check to make sure I’m not off by 1, because I know of this problem :)). I’d love to know how your kids take to it.

i dont get why ppl dont add this as a topic or at least a dedicate a page to this problem in mathematics, over the years i made countless mistakes regarding this, but after reading this it not only was my confusion totally dissolved i knew how to tackle the problem with ease, i mean you sometimes get long questions involving calculus and quadractic equations which you need to construst from the context of the questions and you usually don’t account for the fencepost problem ( not becuz its hard but becuz no one told you that this could be a problem !! ), seriously someone should inform the authorities about this.

@Joy: This is a problem that hits almost every programmer (“off by one” errors) and yeah, until you know it’s a problem it can be so easy to get bitten.

You’ve also just explained the difference between Natural Numbers, and Integers.

i have 65 ft of fence the panels can not be longer than 70 in AT WHAT DISTANCE DO I PUT THE POSTS TO ALLOW AN EQUAL NUMBER OF 5 1/2 IN BOARDS WITH EQUAL SPACING AND 4 IN POSTS THANKS

Fixed, like, 99% of my calculus class problems.

I teach programming, and have to explain indexing from zero to incredulous students a few times a year. It doesn’t help that some languages index from one (e.g. nth-child in css) whereas most index from zero.

I point out that even though we learn to count on our fingers, where the first number was one, everybody celebrated big time on the eve of the year 2000, whereas the true start of the new century was in fact 1st January 2001.

So no matter how strange it seems at first, we number years, centuries etc. from zero, but days of the month from 1. This is why the 20th century is the one where all the years, except the last, start with a 19.

Also, educational toys, videos, games and books for small children almost always skip zero altogether. I regard this as a mistake. Do they think they are protecting the kids from something?

I suspect we could clear up some (but not all) of these off-by-one issues if we actually had a commonly accepted way to say and write 0th. “Zeroth”? “Nullth”? “Nilth”? “Naughtth” “Noneth”? We can’t even agree on the most proper way to say 0.

Even programmers well versed in zero-indexed arrays call this index “First”, invariably abbreviated to 1st – uh-oh, there’s confusion already. It’s certainly not pronounced “Wunst”.

The Aha! moment for me was when I realized span is the line between two end-points. For a single line (1 span), there’re two end points (2 items); many times when we’re counting, we just go with the span calculation, and fail to add one more, the beginning point.

I frequently made error in boundry conditions during programming. Now I know why :). Great Article. Thanks

Great article. I was looking for a good explanation to point others to, this is definitely it.

On a similar note, I ran into the following problem a while ago:

Q: Given a month (0-based, i.e. 0=January, 11=December), return the quarter that the month lies in (again 0-based, i.e. 0=1st quarter, 1=2nd quarter etc).

A: Intuitively, you would divide the month by 4 to get the quarter. But that’s wrong, since 3/4 yields 0 (integer arithmetic), but 3 (April) should be in the 2nd quarter (1), not in the first quarter (0)! So instead of dividing by four you’ll have to divide by something that yields four pieces (i.e. 12/4=3).

@Bob: Thanks for the comment!