beanz Magazine

The Hungry Camel

Humphrey the Camel on Flickr

How many measures of grain can one camel eat while delivering grain, before the camel runs out of grain to deliver? A fun math problem at least 1,000 years old.

Recreational math problems and puzzles are key to learning how to solve problems. The ability to carefully tease out an answer from a complicated problem, reducing complexity and confusion to the simplest possible units, also is key to learning computer science and programming.

Every issue of this magazine will have at least one puzzle and math problem. Solutions will be offered at the bottom of the page but you have to promise not to to cheat and look up the answer. It's the honor system.

Here is the puzzle, as written down in England around 800 AD:

A certain head of household ordered that 90 measures of grain be taken from one of his houses to another 30 leagues away. This load of grain can be carried by a camel in three trips. Given that the camel eats one measure of grain for each league it goes (the camel only eats when carrying a load), how many measures were left after the grain was transported to the second house?

Like many math puzzles told as a story, the details are deliberately confusing on the surface. To solve this problem, you need to break out the story details, find any hidden assumptions, and create a strategy to solve the problem. This is the same approach people take when trying to solve a real life problem with software programming.

Here are the details from the story, what we know:

  • There are 90 measures of grain to be transported.
  • The grain is to be transported 30 leagues away.
  • A camel can carry the 90 measures of grain in 3 trips. Therefore, the camel can carry 30 measures of grain each trip, as a maximum limit. The camel also will travel a total of 90 leagues, three trips of 30 leagues each, to carry the 90 measures of grain.
  • A camel eats 1 measure of grain for every league it carries a load. Therefore, a 30 league trip will require 30 measures of grain to feed the camel and a 90 league trip will require 90 measures of grain to feed the presumably still hungry camel.

The two “therefore” statements indicate hidden assumptions in the math puzzle. These are key details. Because these details are not described, the puzzle seems harder than it is.

On the surface, the obvious answer is, there will be no grain left: the camel will carry 90 measures of grain a total of 90 leagues, eating one measure of grain for each of the 90 leagues it travels. Done. You can go home now.

However, that’s a deliberate false ending. Think for a moment about the problem: would you send 90 measures of grain this way, knowing your camel will eat it all? Probably not since it would be a waste of grain and time.

The problem is not “how much grain will be left?” The problem is, “how do you get as much grain as possible to your destination, knowing the camel will eat most of your grain?”

The solution hinges on where the camel stops, turns around, and heads back to get the second and third loads. If the camel travels 30 leagues then returns for the next load, we know all the grain will be eaten. But what happens if the camel stops at 20 leagues and returns for the next load, how much grain will be left? What happens if the camel stops at 10 leagues, will any grain be left?

Let’s model both scenarios.

If the camel travels only 20 leagues carrying 30 measures of grain, and eating one measure of grain per league for a total of 20 measures of grain, there will be 10 measures of grain left after each 20 league trip. After three trips of 20 leagues, there will be 30 measures of grain to carry the final 10 leagues, a single load for our camel. Since the camel eats 1 measure of grain for every league it travels, this final 10 league trip will yield 10 measures of grain eaten by our camel and 20 measures of grain for the farmer.

If the camel only travels 10 leagues carrying 30 measures of grain, eating one measure of grain per league for a total of 10 measures of grain eaten per trip, there will be 20 measures of grain left after each of the three trips, for a total of 60 measures of grain. But you still have to travel another 20 leagues. And since each trip can only carry 30 measures of grain, and you have 60 measures of grain left at the 10 league mark, you need two more trips for the final 20 leagues. The camel needs to travel 40 more leagues which means 40 more measures of grain our hungry camel will eat. You’ll be left with 20 measures of grain after two more trips.

It appears the best we can do is save 20 measures of grain this way.

However, is there a better answer? Let’s apply our strategy, which has recovered 20 measures of grain so far, to the problem. If it makes no difference whether our camel turns around at 20 leagues or 10 leagues, both result in 20 measures of grain saved, let’s start with our camel turning around at 10 leagues. That leaves us 20 leagues to figure out if turning the camel around a second time saves us any more grain.

We’ll work with 5 league increments, starting from our 30 league end point.

So let’s begin by having our camel turn around at 10 leagues and then again at 25 leagues. Here’s how the problem works out:

  • At 10 leagues, our camel has eaten 10 measures of grain and left us 20 measures of grain. After three 10 league trips, we have 60 measures of grain left. Since our load limit is 30 measures of grain per trip, we only need to two more trips.
  • We have 20 more leagues to carry our 60 measures of grain. To stop at the 25 league mark, we need to travel an additional 15 leagues from our 10 league stop point.
  • To travel the 15 leagues, to the 25 league stop point, we’ll have to feed our hungry camel 15 measures of grain. That will leave us 15 measures of grain per trip.
  • After the second 15 league trip, we will have 30 measures of grain to carry the last 5 leagues. If you recall, 30 measures of grain is the load limit for our camel.
  • Carrying the last load of 30 measures of grain the last 5 leagues will use up 5 measures of grain to feed the camel. This leaves us 25 measures of grain, 5 more than if we stopped the camel at the 10 leagues and 20 leagues and returned to the start point to reload.

Clearly, patience and persistence work: we have an additional 5 measures of grain if we stop our camel at the 10 league and 25 league point to return and reload.

Is this the best we can do? Let’s do one more thought exercise, this time stopping and reloading at the 10 league and 20 league points. Here’s how it would work out:

  • At 10 leagues, our camel has eaten 10 measures of grain and left us 20 measures of grain. After three 10 league trips, we have 60 measures of grain left. Since our load limit is 30 measures of grain per trip, we only need to two more trips.
  • We have 20 more leagues to carry our 60 measures of grain. To stop at the 20 league mark, we need to travel an additional 10 leagues from our 10 league stop point.
  • To travel the 10 leagues, to the 20 league stop point, we’ll have to feed our hungry camel 10 measures of grain. That will leave us 20 measures of grain per trip.
  • After the second 10 league trip, we will have 40 measures of grain to carry the last 10 leagues. If you recall, 30 measures of grain is the load limit for our camel. So we will need two trips to get from 20 leagues to 30 leagues.
  • Because the camel will eat 10 measures of grain for each 10 league trip, for a total of 20 measures of grain eaten, we will be left with 20 measures of grain, the same as if we stopped only once, at 10 leagues or 20 leagues.

Our model solutions show the best solution is to have our camel stop at the 10 league and 25 league points to give us 25 measures of grain saved from our hungry camel. Three 30 league trips fully loaded yields no grain because the camel will eat it all. And stopping once at the 10 league or 20 league point yields only 20 measures of grain.

This problem is a great example how to solve complicated problems, mostly by writing down all the details, teasing out all the assumptions, and then working different ways to solve a problem. Software programming problems often have multiple solutions where the ability to clearly define the problem, find hidden assumptions, and create possible solutions are the best way to write great code.

Most interesting of all, there are times when the nature of a programming language dictates which solution works best. It’s possible there are two possible ways to code, for example, but one way of coding is faster than another. How do you find these subtleties? You ask other coders, in person or online. Or someone happens to look at your code and makes the suggestion. It’s one way programming is a treasure hunt, a journey, not an activity with obvious answers in plain sight.

Last, and not least, this math problem was found in a book written around 800 AD and given to Charlemagne by a British scholar, Alcuin. There are not many camels in Britain, are there? Likely this math problem came from some place south of the British Isles even before Alcuin wrote it down. Apparently camels have always been hungry enough to eat what they carry.

If you’re interested in the handsome camel in the photo at the top of the story, there’s a link below.

Learn More

Camel at the top of Cribyn, WaterAid Corbett Challenge – See more at: http://humphreythecamel.org/camel-diary/camel-at-the-top-of-cribyn-wateraid-corbett-challenge/

http://humphreythecamel.org/camel-diary/camel-at-the-top-of-cribyn-wateraid-corbett-challenge/

Also In The December 2013 Issue

An Interview with Troy Hunt

Troy Hunt is a software architect and Microsoft Most Valued Professional (MVP) focusing on security concepts and process improvement in a Fortune 50 company. He's based in Australia.

1Password, LastPass, RoboForm

If you use a password you created that is less than eight characters, your password is vulnerable to hacking. Here are three ways to create and use secure passwords online.

How to Write Secure Code

Coding securely doesn't have to kill the joy of programming. In fact, learning how to code securely provides insights into languages and computing.

How to Code HTML Email

How to code an HTML email like the ones you open every day turns out to be an offbeat software coding challenge.

What is an SSL Certificate?

How to tell if a web page is secure is one of the most basic yet least obvious ways to protect your data online.

Where to Find Command Line Interface Software

One key computing skill is the ability to use command line interface (CLI) software to enter commands to control a computer. Here are some options.

Lua

Lua is a comparatively simple programming language used in a wide range of places, from digital TVs to video games to phone applications. It's also designed to be simple to use and lightweight.

Arrays

Here is how three programming languages handle a common problem: how do you organize and keep track of useful data?

Linux Command List for Command Line Interfaces

Some of the most common commands you'll need for a command line interface (CLI), in a Linux command list.

Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.

News Wire Stories for October 2013

Must read stories about computer science, software programming, and technology for September 2013.

Learn More Links for October 2013

Links from the bottom of all the October 2013 articles, collected in one place for you to print, share, or bookmark.