A programmer’s view of an old story.

The story was of a merchant, or hero, who declared that the payment that they wanted was a grain of rice on the first day, and double that number every day. So day 1 the person would receive 1 grain, and on day two 2, giving him a total of 3 grains.

Well, if you continue this out 31 days, you hit a gain of 2,147,483,647 grains. This weighs in at 68.6 tons.

So this is the progression of gain:

  • Day 1 has gained 1 grains, totaling at 1. Weighing in at 0.029 grams, and at 0 Tons.
  • Day 2 has gained 2 grains, totaling at 3. Weighing in at 0.087 grams, and at 0 Tons.
  • Day 3 has gained 4 grains, totaling at 7. Weighing in at 0.203 grams, and at 0 Tons.
  • Day 4 has gained 8 grains, totaling at 15. Weighing in at 0.435 grams, and at 0 Tons.
  • Day 5 has gained 16 grains, totaling at 31. Weighing in at 0.899 grams, and at 0 Tons.
  • Day 6 has gained 32 grains, totaling at 63. Weighing in at 1.827 grams, and at 0 Tons.
  • Day 7 has gained 64 grains, totaling at 127. Weighing in at 3.683 grams, and at 0 Tons.
  • Day 8 has gained 128 grains, totaling at 255. Weighing in at 7.395 grams, and at 0 Tons.
  • Day 9 has gained 256 grains, totaling at 511. Weighing in at 14.819 grams, and at 0 Tons.
  • Day 10 has gained 512 grains, totaling at 1023. Weighing in at 29.667 grams, and at 0 Tons.
  • Day 11 has gained 1024 grains, totaling at 2047. Weighing in at 59.363 grams, and at 0 Tons.
  • Day 12 has gained 2048 grains, totaling at 4095. Weighing in at 118.755 grams, and at 0 Tons.
  • Day 13 has gained 4096 grains, totaling at 8191. Weighing in at 237.539 grams, and at 0 Tons.
  • Day 14 has gained 8192 grains, totaling at 16383. Weighing in at 475.107 grams, and at 0.001 Tons.
  • Day 15 has gained 16384 grains, totaling at 32767. Weighing in at 950.243 grams, and at 0.001 Tons.
  • Day 16 has gained 32768 grains, totaling at 65535. Weighing in at 1900.515 grams, and at 0.002 Tons.
  • Day 17 has gained 65536 grains, totaling at 131071. Weighing in at 3801.059 grams, and at 0.004 Tons.
  • Day 18 has gained 131072 grains, totaling at 262143. Weighing in at 7602.147 grams, and at 0.008 Tons.
  • Day 19 has gained 262144 grains, totaling at 524287. Weighing in at 15204.323 grams, and at 0.017 Tons.
  • Day 20 has gained 524288 grains, totaling at 1048575. Weighing in at 30408.675 grams, and at 0.034 Tons.
  • Day 21 has gained 1048576 grains, totaling at 2097151. Weighing in at 60817.379 grams, and at 0.067 Tons.
  • Day 22 has gained 2097152 grains, totaling at 4194303. Weighing in at 121634.787 grams, and at 0.134 Tons.
  • Day 23 has gained 4194304 grains, totaling at 8388607. Weighing in at 243269.603 grams, and at 0.268 Tons.
  • Day 24 has gained 8388608 grains, totaling at 16777215. Weighing in at 486539.235 grams, and at 0.536 Tons.
  • Day 25 has gained 16777216 grains, totaling at 33554431. Weighing in at 973078.499 grams, and at 1.073 Tons.
  • Day 26 has gained 33554432 grains, totaling at 67108863. Weighing in at 1946157.027 grams, and at 2.145 Tons.
  • Day 27 has gained 67108864 grains, totaling at 134217727. Weighing in at 3892314.083 grams, and at 4.291 Tons.
  • Day 28 has gained 134217728 grains, totaling at 268435455. Weighing in at 7784628.195 grams, and at 8.581 Tons.
  • Day 29 has gained 268435456 grains, totaling at 536870911. Weighing in at 15569256.419 grams, and at 17.162 Tons.
  • Day 30 has gained 536870912 grains, totaling at 1073741823. Weighing in at 31138512.867 grams, and at 34.324 Tons.
  • Day 31 has gained 1073741824 grains, totaling at 2147483647. Weighing in at 62277025.763 grams, and at 68.649 Tons.

If I had 69 tons of rice, I tell you, I’d be in pretty good financial shape for a short time.

So here is the code, because I think if you made it here, you are curious. Please note, this can be made more efficient.

//The exponential rice story in formula form.

ulong output = 0;
uint maxDay = 31;
ulong nextRiceCount = 1;
decimal eachGrainOfRiceInGrams = 0.029m;
decimal eachGrainOfRiceInTons = eachGrainOfRiceInGrams / 907184.74m;

// Question: how much rice would you have if every day someone
// had to double the rice that they gave you if they started
// at 1 and doubled their donation each day.

for (uint currentDay = 1; currentDay <= maxDay; currentDay++)
{
output = output + nextRiceCount;
Console.Write($”Day {currentDay} has gained {nextRiceCount} grains, totaling at {output}.”);
nextRiceCount = nextRiceCount * 2;
Console.Write($” Weighing in at {output * eachGrainOfRiceInGrams} grams,”);
Console.WriteLine($” and at {Math.Round( output * eachGrainOfRiceInTons * 1000) / 1000} Tons.”);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.