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 that is 10 feet long needs 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.

## Other Posts In This Series

- Techniques for Adding the Numbers 1 to 100
- Rethinking Arithmetic: A Visual Guide
- Quick Insight: Intuitive Meaning of Division
- Quick Insight: Subtracting Negative Numbers
- Surprising Patterns in the Square Numbers (1, 4, 9, 16…)
- Fun With Modular Arithmetic
- Learning How to Count (Avoiding The Fencepost Problem)
- A Quirky Introduction To Number Systems
- Another Look at Prime Numbers
- Intuition For The Golden Ratio
- Different Interpretations for the Number Zero

JakeApril 28, 2009 at 6:46 amYour 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.

SlariApril 28, 2009 at 6:50 amIt looks like a copy-paste from Word.

MichaelApril 28, 2009 at 7:05 amhttp://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.

KalidApril 28, 2009 at 10:12 amThanks 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.

KalidApril 28, 2009 at 10:28 amOk, the weird characters should be fixed. Thanks again for the notice everyone!

PhilApril 28, 2009 at 12:48 pmThat’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).

Marian DvorskyApril 28, 2009 at 12:55 pmThe 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.

Marian DvorskyApril 28, 2009 at 12:58 pmThe code snippet in my comment above should read:

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

…

Sorry about that.

Scientific ChickApril 28, 2009 at 1:51 pmInteresting post on a common issue. Thanks!

QuerymanApril 28, 2009 at 10:43 pmThanks 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."

IsmeetApril 29, 2009 at 4:53 amHey,

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. :)

Tiago S.April 29, 2009 at 11:48 amThanks for the article. I love when you post new stuff.

Please, keep posting :-)

CLApril 29, 2009 at 12:40 pmGreat 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!

KalidApril 29, 2009 at 10:47 pm@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 :).

jehanApril 30, 2009 at 7:09 amOMG! 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!

NobodyMay 1, 2009 at 8:01 pmMusic 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.

KalidMay 5, 2009 at 12:36 am@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.

andrewMay 6, 2009 at 8:48 amthis post reminded me of an old puzzle:

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

BrianMay 16, 2009 at 9:29 pmI 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.

KalidMay 17, 2009 at 2:06 pm@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!

NobodyMay 18, 2009 at 2:51 pmAlso 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.

Better Explained . com « Termites in your Smile…June 13, 2009 at 5:08 am[…] I just discovered BetterExplained.com, the brainchild of Kalid Azad. It’s great! Kalid writes clear, thorough explanations of all kinds of ideas from simple stuff to complicated stuff. Math, computers, marketing, communication and more. […]

MitchSeptember 12, 2009 at 2:26 pmFinally, a clear way explanation of why 1 Jan 2000 was not the beginning of the new century (and millennium) – 1 Jan 2001 was!

SahasranandSeptember 14, 2009 at 3:17 amI’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.

mikekoz68October 27, 2009 at 10:19 amAre you sure? I think I can cut a cake into 10 pieces with just 5 cuts.

DaveOctober 31, 2009 at 8:48 amAnother 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!

DaveOctober 31, 2009 at 9:03 amMikekoz-

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

Alexander JennerNovember 14, 2009 at 5:03 amHi 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.

KalidNovember 17, 2009 at 8:37 pm@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” :).

how to count « a Silent ParrotMarch 4, 2010 at 2:56 pm[…] Learning how to count (avoiding the fence post problem) […]

BradJune 8, 2010 at 1:56 pmGreat 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.

udc web » Learning to CountOctober 13, 2010 at 8:32 am[…] 8.Learning How to Count (Avoiding The Fencepost Problem) | 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. … Pingback by Topics about Aprils-day » Learning How to Count (Avoiding The Fencepost Problem … — April 28, 2009 @ 6:58 pm… http://betterexplained.com/articles/learning-how-to-count-avoiding-the-fencepost-problem/ […]

RebeccaOctober 20, 2010 at 6:09 amThanks 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.

KalidOctober 21, 2010 at 11:35 pm@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.

JoyJanuary 16, 2011 at 4:43 pmi 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.

KalidJanuary 24, 2011 at 3:29 pm@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.

AlexMay 24, 2011 at 1:55 pmYou’ve also just explained the difference between Natural Numbers, and Integers.

MITCH ATKINSONAugust 11, 2012 at 9:00 pmi 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

BestormJanuary 8, 2013 at 5:42 pmFixed, like, 99% of my calculus class problems.

Brennan YoungDecember 26, 2013 at 6:25 amI 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”.

SundaramJuly 3, 2014 at 7:49 amThe 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.

KunalJanuary 5, 2015 at 2:13 pmI frequently made error in boundry conditions during programming. Now I know why :). Great Article. Thanks

How to Raise Test Scores: Read Carefully! | Math Confidence by Robin the Math LadyFebruary 15, 2015 at 10:22 am[…] good wrong answer because she forgot about the first number in Jill’s pattern. This is a Fencepost mistake (for example, for a 100 foot fence with posts every 10 feet, you need 11 fence […]

Bob LauerApril 30, 2015 at 7:18 amGreat 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).

kalidApril 30, 2015 at 7:26 pm@Bob: Thanks for the comment!

BhaskarOctober 15, 2015 at 11:57 amErrata on line:

I’d double-check with a smaller example – a fence of length 10 would have 2 posts, so yes, we need an “extra post”.

Should read “a fence of length{\bf 1}” instead of “a fence of length {\bf 10}”

kalidOctober 17, 2015 at 7:48 pmHi Bhaskar, thanks for the note. I clarified to say a fence of 10 feet needs two posts (assuming we need a post every 10 feet). Appreciate it!

SidJuly 16, 2016 at 12:32 pmAnother example of the fencepost problem? I have to move three cars from where I live now, where I start this process, to where I am moving to, where I will end this process. I carry a bicycle with me each time so I can bicycle back to get the next car. How many times must I ride the bicycle?

AshleySeptember 20, 2016 at 7:31 amWow, I’ve been encountering this for years (I’m currently mid-way through a computer science degree myself), but always thought it was a problem with my understanding that I’d have to think twice about subtracting to get quantities, and how it wouldn’t always seem immediately intuitive. Thanks for reassuring me that it’s not just me – every time I’ve tried querying others to see if they have a similar issue, I’d phrase it badly. It’s great to know that this is a common difficulty! :)

Loving your blog, thanks so much!

adamOctober 17, 2016 at 11:13 amThank you from the bottom of my heart

SwickyMarch 6, 2017 at 12:06 pmI also like to use a “Loaf” analogy. Each slice of a loaf is an item, each cut is a segment. That first piece of bread is the part most don’t count.

EminMarch 17, 2017 at 5:21 amLovely article.

Impaled on a Fencepost |November 16, 2017 at 3:04 pm[…] love Kalid Azad’s explanation of fencepost error at https://betterexplained.com/. It has a great opening […]

Alex HertelMay 5, 2018 at 11:53 pmAnother example of fencepost counting is birthdays. When a kid turns 4 years old, or when someone turns 65 and retires, people mistakenly say that’s the kid’s “4th birthday” or the retiree’s “65th birthday”, but this again is the fencepost problem. They’re forgetting that your first birthday is on the day that you’re born (because that’s literally your BIRTH DAY), so when you turn 4 years old, that’s actually your 5th birthday. In other words, “4 years old” refers to a span whereas “4th birthday” refers to the fenceposts.

Similarly, centuries are spans, so the year 1972 happened during the 20th century. Counting really is much more complicated and confusing than most people realize.

asdAugust 22, 2018 at 11:21 amInteresting. The root of the difference seems to be with discrete versus continuous things.