About the author

Steven HarmanSteven Harman is a passionate developer who believes that writing great software isn't just a job, its a craft.

ASP.NET MVP

For recent posts and more about me, scroll to the bottom.

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

News

devLINK Technical Conference

Jobs

Badges

  • Subtext Project
  • Support Subtext
  • HiddenNetwork.com Banner

Learning To Count, Again

Quick, how many discrete values are between zero (0) and one hundred (100)?

I’ll wait here while you count...

Possible correct answers, depending on how you interpreted the question and how you count, are:

  • 99
  • 100
  • 101

Why so many answers?

Fencepost Error This is a classic example of a fencepost error, also commonly known as an off-by-one error. It’s all about how you count!

Let’s discuss each of the above answers one at a time.

Ninety-nine

An answer of 99 indicates that you interpreted the word "between" to mean that we wanted the values from 0-100, exclusive. Meaning, not counting the end points of 0 and 100. In mathematical notation you would write this as (0-100), where the curved-parentheses mean open-ended interval.

One hundred

An answer of 100 is probably the most common answer to this question. Most folks just do a simple subtraction in their head and arrive at 100. Simple, right?

However, that only works if one endpoint is being counted inclusively, and the other exclusively. This is could be written as:

  • (0-100]
  • [0-100)

Where the square parenthesis represents a closed-ended interval, meaning the end point in inclusive to the range.

One hundred one

As you might have guess by now, an answer of 101 would mean you counted the range inclusively, counting both endpoints and each integer in between them. This can be written as [0-100].

Infinity

And finally... ∞. I threw this answer in there for the smart-asses in the crowd who would note that while I did qualify the question with the word discrete, I left it a bit ambiguous by using the word value, rather than something more concrete like integer.

However to quote the source

Discrete mathematics, also called finite mathematics or Decision Maths, is the study of mathematical structures that are fundamentally discrete, in the sense of not supporting or requiring the notion of continuity. Most, if not all, of the objects studied in finite mathematics are countable sets, such as integers, finite graphs, and formal languages.

So I could argue that it was implied that we were only interested in counting the integer values within the range. Either way, I don’t want to start a holy war over semantics and mathematical notation... for Phil and Jeff would likely destroy me. But I digress...

And the point is...?

The point is we, the genius software engineers we are, need to be careful even when it comes to the simple stuff... like counting. Simple counting mistakes like this often lead to some of the hardest bugs to track down - sometimes they are even the root-cause of those phantom bugs that we just can’t explain.

Technorati Tags: , ,

What others are saying.

# re: Learning To Count, Again
Gravatar Rob Conery
Oct 03, 2007
There was a philosopher I studied in college named Zeno who used to like to mess with the sophists by coming up with logical puzzles with no answer.

One of the ones he came up with (that I saw on Cosmos) was the Rock Dropping To The Ground. He stood in front of a group of philosophers and stated that "for this rock to reach the ground, it must first travel half of the total distance to the ground, correct?"

And they all said "yes of course".

He then added "and then from that half way point, it must go halfway again to the ground, and then halfway again, and then again. In fact there are infinite halfway points as this rock travels to the ground, so I must ask you - does the rock reach the ground? And if so - how?"

So whaddya think Harman? Does the rock reach the ground?
# re: Learning To Count, Again
Gravatar Steven Harman
Oct 03, 2007
Dang it @Rob, that's exactly the kind of response I was trying to side-step with all of the discrete mathematics hand-waving. :)

But seriously, I love those kinds of puzzles. They really make you think about just how small, and conversely how large, things can be. And then of course your head explodes when you suddenly realize that some things are so large that they don't have a beginning and and end, like time.

And to answer your question, I would say that this is why things like the laws of physics break down when we start to talk about really big or really small things. For example, how kinds of stuff starts to go awry as an object approaches the speed of light, or when matter gets close to the immeasurable mass (and gravity) of a black hole.

Shit breaks down and then the implausible, happens. :)

Between this and Jeff's post about parallel universes we really are geeking out today.

Love it, live it!
# re: Learning To Count, Again
Gravatar Rob Conery
Oct 03, 2007
Ahhh but there IS an answer! It's a logic puzzle that I won't allow you to side-step!
# re: Learning To Count, Again
Gravatar cliff
Oct 04, 2007
I think Wikipedia's article on convergent series should clear this one up nicely. The fundamental logic flaw here is in assuming that an infinite sum - i.e., a sum of infinitely many elements - must necessarily produce an infinite result. However, this is not the case.

One less "correct" (but perhaps more intuitive) responses could be "eventually the rock gets so close to the ground that it might as well be resting on it". Alternately, for the geek types (I assume there are a few who read this blog), there is always "there isn't enough floating point precision to represent the problem - the rock will just run out of bits, eventually". :-)
# re: Learning To Count, Again
Gravatar Ryan
Oct 22, 2007
Although you didn't mention it, the counting reminds me of C strings -- don't forget to add an element in the array for the NULL termination!
Comments have been closed on this topic.