A few hundred years ago the glorious leader of a big country wanted to reward the creator of the chess game. The creator of the game simply wanted one grain of rice put on the first square of the chessboard, two grains on the second, then doubling it for every remaining square. The glorious leader had one thing in common with the our present day leaders, he had no idea about basic mathematical concepts. So he acted surprised by the humble request, had his helpers bring a bag of rice and started to fill the squares.

Did the creator of the chess game get a decent reward? Here is a simple Python program to do the counting.

Corrected the code after looking at the loop result prints.

Edited 1 Year Ago by vegaseat: loop correction

''' chessboard_grains.py
putting one grain of rice on the first square of a chessboard
two grains of rice on the second square
and doubling it every remaining square

some Google facts ...
29,000 grains of rice per pound
rice production world wide (2012) = 740,000,000 tonnes
2,200 pounds per tonne

tested with Python34  by  vegaseat   09may2015
'''

print("Grains of rice on a chessboard (doubling every square):\n")

grains_sum = 0
grains = 1
for square in range(1, 64+1):
    grains_sum += grains
    print("square {}  grains = {}  total= {}".format(square, grains, grains_sum))
    grains *= 2

print('-'*56)

# test, another way to calculate the grains_sum
print("2**64 - 1    = {}".format(2**64 - 1))
print("Total grains = {}".format(grains_sum) )

print("Assume 29000 grains per pound of rice:")
pounds_req = grains_sum//29000
print("{:,} pounds".format(pounds_req))

print('-'*56)

print("Worldwide Rice Production in 2012: 740,000,000 tonnes or")
prod = 740000000 * 2200
print("{:,} pounds".format(prod))
print("The requested {:,} pounds would consume".format(pounds_req))
print("{:,d} world productions (2012)".format(int(1.0*pounds_req/prod)))

''' result ...
square 1  grains = 1  total= 1
square 2  grains = 2  total= 3
square 3  grains = 4  total= 7
square 4  grains = 8  total= 15
square 5  grains = 16  total= 31
square 6  grains = 32  total= 63
square 7  grains = 64  total= 127
square 8  grains = 128  total= 255
square 9  grains = 256  total= 511
square 10  grains = 512  total= 1023
square 11  grains = 1024  total= 2047
square 12  grains = 2048  total= 4095
square 13  grains = 4096  total= 8191
square 14  grains = 8192  total= 16383
square 15  grains = 16384  total= 32767
square 16  grains = 32768  total= 65535
square 17  grains = 65536  total= 131071
square 18  grains = 131072  total= 262143
square 19  grains = 262144  total= 524287
square 20  grains = 524288  total= 1048575
square 21  grains = 1048576  total= 2097151
square 22  grains = 2097152  total= 4194303
square 23  grains = 4194304  total= 8388607
square 24  grains = 8388608  total= 16777215
square 25  grains = 16777216  total= 33554431
square 26  grains = 33554432  total= 67108863
square 27  grains = 67108864  total= 134217727
square 28  grains = 134217728  total= 268435455
square 29  grains = 268435456  total= 536870911
square 30  grains = 536870912  total= 1073741823
square 31  grains = 1073741824  total= 2147483647
square 32  grains = 2147483648  total= 4294967295
square 33  grains = 4294967296  total= 8589934591
square 34  grains = 8589934592  total= 17179869183
square 35  grains = 17179869184  total= 34359738367
square 36  grains = 34359738368  total= 68719476735
square 37  grains = 68719476736  total= 137438953471
square 38  grains = 137438953472  total= 274877906943
square 39  grains = 274877906944  total= 549755813887
square 40  grains = 549755813888  total= 1099511627775
square 41  grains = 1099511627776  total= 2199023255551
square 42  grains = 2199023255552  total= 4398046511103
square 43  grains = 4398046511104  total= 8796093022207
square 44  grains = 8796093022208  total= 17592186044415
square 45  grains = 17592186044416  total= 35184372088831
square 46  grains = 35184372088832  total= 70368744177663
square 47  grains = 70368744177664  total= 140737488355327
square 48  grains = 140737488355328  total= 281474976710655
square 49  grains = 281474976710656  total= 562949953421311
square 50  grains = 562949953421312  total= 1125899906842623
square 51  grains = 1125899906842624  total= 2251799813685247
square 52  grains = 2251799813685248  total= 4503599627370495
square 53  grains = 4503599627370496  total= 9007199254740991
square 54  grains = 9007199254740992  total= 18014398509481983
square 55  grains = 18014398509481984  total= 36028797018963967
square 56  grains = 36028797018963968  total= 72057594037927935
square 57  grains = 72057594037927936  total= 144115188075855871
square 58  grains = 144115188075855872  total= 288230376151711743
square 59  grains = 288230376151711744  total= 576460752303423487
square 60  grains = 576460752303423488  total= 1152921504606846975
square 61  grains = 1152921504606846976  total= 2305843009213693951
square 62  grains = 2305843009213693952  total= 4611686018427387903
square 63  grains = 4611686018427387904  total= 9223372036854775807
square 64  grains = 9223372036854775808  total= 18446744073709551615
--------------------------------------------------------
2**64 - 1    = 18446744073709551615
Total grains = 18446744073709551615
Assume 29000 grains per pound of rice:
636,094,623,231,363 pounds
--------------------------------------------------------
Worldwide Rice Production in 2012: 740,000,000 tonnes or
1,628,000,000,000 pounds
The requested 636,094,623,231,363 pounds would consume
390 world productions (2012)
'''

Oh, this is a good one. I remember it when my high school teacher was telling us the story!

Thank you so much Gribouillis!
I made the corrrection.

Looks like I lack the basic mathematical concepts too!

Edited 1 Year Ago by vegaseat

If you look at the loop results carefully, you will discover that the number of grains on each square 1 to 64 is equal to 2**(square-1) and the number of total accumulated grains at this point is (2**square) - 1

Here is some code ...

''' chessboard_grains2.py
putting one grain of rice on the first square of a chessboard
two grains of rice on the second square
and doubling it every remaining square

tested with Python34  by  vegaseat   13may2015
'''

for square in range(1, 64+1):
    grains = 2**(square-1)
    grains_sum = (2**square) - 1
    print("square {}  grains = {}  total= {}".format(square, grains, grains_sum))

''' result ...
square 1  grains = 1  total= 1
square 2  grains = 2  total= 3
square 3  grains = 4  total= 7
square 4  grains = 8  total= 15
square 5  grains = 16  total= 31
square 6  grains = 32  total= 63
square 7  grains = 64  total= 127
square 8  grains = 128  total= 255
square 9  grains = 256  total= 511
square 10  grains = 512  total= 1023
square 11  grains = 1024  total= 2047
square 12  grains = 2048  total= 4095
square 13  grains = 4096  total= 8191
square 14  grains = 8192  total= 16383
square 15  grains = 16384  total= 32767
square 16  grains = 32768  total= 65535
square 17  grains = 65536  total= 131071
square 18  grains = 131072  total= 262143
square 19  grains = 262144  total= 524287
square 20  grains = 524288  total= 1048575
square 21  grains = 1048576  total= 2097151
square 22  grains = 2097152  total= 4194303
square 23  grains = 4194304  total= 8388607
square 24  grains = 8388608  total= 16777215
square 25  grains = 16777216  total= 33554431
square 26  grains = 33554432  total= 67108863
square 27  grains = 67108864  total= 134217727
square 28  grains = 134217728  total= 268435455
square 29  grains = 268435456  total= 536870911
square 30  grains = 536870912  total= 1073741823
square 31  grains = 1073741824  total= 2147483647
square 32  grains = 2147483648  total= 4294967295
square 33  grains = 4294967296  total= 8589934591
square 34  grains = 8589934592  total= 17179869183
square 35  grains = 17179869184  total= 34359738367
square 36  grains = 34359738368  total= 68719476735
square 37  grains = 68719476736  total= 137438953471
square 38  grains = 137438953472  total= 274877906943
square 39  grains = 274877906944  total= 549755813887
square 40  grains = 549755813888  total= 1099511627775
square 41  grains = 1099511627776  total= 2199023255551
square 42  grains = 2199023255552  total= 4398046511103
square 43  grains = 4398046511104  total= 8796093022207
square 44  grains = 8796093022208  total= 17592186044415
square 45  grains = 17592186044416  total= 35184372088831
square 46  grains = 35184372088832  total= 70368744177663
square 47  grains = 70368744177664  total= 140737488355327
square 48  grains = 140737488355328  total= 281474976710655
square 49  grains = 281474976710656  total= 562949953421311
square 50  grains = 562949953421312  total= 1125899906842623
square 51  grains = 1125899906842624  total= 2251799813685247
square 52  grains = 2251799813685248  total= 4503599627370495
square 53  grains = 4503599627370496  total= 9007199254740991
square 54  grains = 9007199254740992  total= 18014398509481983
square 55  grains = 18014398509481984  total= 36028797018963967
square 56  grains = 36028797018963968  total= 72057594037927935
square 57  grains = 72057594037927936  total= 144115188075855871
square 58  grains = 144115188075855872  total= 288230376151711743
square 59  grains = 288230376151711744  total= 576460752303423487
square 60  grains = 576460752303423488  total= 1152921504606846975
square 61  grains = 1152921504606846976  total= 2305843009213693951
square 62  grains = 2305843009213693952  total= 4611686018427387903
square 63  grains = 4611686018427387904  total= 9223372036854775807
square 64  grains = 9223372036854775808  total= 18446744073709551615
'''

Simple. The number of grains on any square == (2 ^ square number) - 1. I leave the computation for the total number of grains on all squares up to the max as an exercise! :-)

FWIW, this kind of reminds me of that old quip, "How many pounds are in a fortnight?". :LOL:

And if you don't get the joke, just ask someone from England! I'm not from there, but I spent a year there when I was a teenager, so I learned a lot of those things. :-)

FWIW, a fortnight is 15 days.

Edited 1 Year Ago by rubberman

Hmmm,

>>> square_number = 64
>>> (2^square_number) - 1
65

Edited 1 Year Ago by vegaseat

Comments
I think he meant it as to the power of , not xor =)

Ah, replace '^' with '**'. I am a little dense sometimes, now I see the humor ...

square_number = 4
grains = (2**square_number) - 1
if grains == 15:
    print("It's a fortnight!")

Note: square number 4 should have 8 grains on it

Edited 1 Year Ago by vegaseat

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.