We’ve all heard the story in school — when chess was invented in India the local King (they had lots of kings back then apparently) was so impressed by the game that he offered its creator whatever he wanted. The man (who was rumored to be wise but somewhat of a smart-ass) responded that all he wanted were a few grains of rice. The number of grains was to be determined as follows: using a chessboard, place 1 grain on the first square, two on the second, four on the third, 8 on the fourth, etc. To which the King immediately assented, until one of his ministers told him that not only was this more than he had to give, but was in fact more grain that was produced by the entire known world at the time.
So, being somewhat of a smart-ass myself and being in the middle of learning the python programming language I figured I’d come up with a neat and efficient way to calculate exactly what that number was. It’s not that difficult. Basically because the chessboard has 64 squares, the number you’re looking for is the sum of the solutions for 20 through 263.
Since Python is loosely-typed you don’t have to worry about declaring the right type of integer to hold the final result, which is astronomically big. Here’s the code for it:
numTotal = 0; for numExponent in range (0,64): numTotal += 2**numExponent print "Total at", numExponent, ": ",numTotal print "All done."
Note: even though I declared the range 0 to 64, 64 is not itself included.
So what’s the final result? It’s 18446744073709551615. 18 quintillion, 445 quadrillion, 744 trillion, 73 billion, 709 million, 551 thousand and 615. Quite a large number indeed and assuredly more than the King could deliver.