## Wednesday, November 25, 2009

### All the rice in China

You may have heard the story about the man who it is said invented the game of chequers. According to the story he was from China. It may also have happened in India and the game may have been chess. In that case the story is known as The King's Chess Board. The emperor or king liked the game so much he decided to reward the inventor.

"What would you like," the emperor asked the man, beaming benignly.
"Your highness," the man humbly said, "let's take a chequer board, put one grain of rice on the first square, two on the second square, four on the next square and so on. We double the grains for every subsequent square. Then your grace can give me all the rice on the board."
"Is that all?" asked the amazed emperor. "No man, choose something of value."
"Your highness," the man said, politely looking at the emperor and measuring his words. "That is not a little bit of rice."

The emperor summoned his accountants and they worked on the problem for two full days. When all the calculations were done they found that there was not enough rice in all of China to give to the man.

When I first heard this story I wondered if this was true. Well, let's see.

Here is a very small Groovy script which does all the heavy lifting for us:
`BigInteger s = 01.upto(63){x ->  s += 2**x } def rice = s + 1`

We start with the second square with two grains of rice on it, that is two to the power of one. We increment the power with one for every square, in effect doubling the grains of rice on each subsequent square. We sum the grains as we go. In the end we add the one grain of rice on the first square. Can't forget that. We don't want to cheat the poor man. Yes, hard to believe, but those few lines above do all that.

Next we apply what we got. I used values from the Internet. The low specific gravity is for rough rice. Most likely the rice in China so long ago was not husked.

This is what we did, again in Groovy:
` println "There are \${rice} grains of rice on the board" println "At 200 grains of rice to a teaspoon that would give \${rice/200} teaspoonfuls of rice" println "To get to liters we multiply by 5 and divide by 1000 : \${rice/200*5/1000}" println "To get to kilograms we multiply by the specific gravity of rice, about 0.58 : \${rice/200*5/1000*0.58}Kg" println "Metric tons of rice on the board: \${rice/200*5/1000*0.58/1000} Metric Tons" println "Total global annual rice production, about 12 million tons" println "Ratio of rice on the board to global annual rice production: \${rice/200*5/1000*0.58/1000/12000000}"`

And here is the print out:
`There are 18446744073709551614 grains of rice on the boardAt 200 grains of rice to a teaspoon that would give 92233720368547758.07 teaspoonfuls of riceTo get to liters we multiply by 5 and divide by 1000 : 461168601842738.79035To get to kilograms we multiply by the specific gravity of rice, about 0.58 : 267477789068788.4984030KgMetric tons of rice on the board: 267477789068.788498403 Metric TonsTotal global annual rice production, about 12 million tonsRatio of rice on the board to global annual rice production: 22289.8157557324`

Absolutely amazing, isn't it? There was 22,290 times as much rice on the board as the global annual rice production!

The story doesn't say if the man got his rice, but I doubt it.

Till next time.