Write a python code for the following:
There is a famous legend about the origin of chess that goes
something like this. When the inventor of the game showed it to the
emperor of India, the emperor was so impressed by the new game,
that he said to the man "name your reward." The man responded, "oh
emperor, my wishes are simple. I only wish for this. Give me one
grain of rice for the first square of the chessboard, two trains
for the next square, four for the next, eight for the next and so
on for all 64 squares, with each square having double the numbert
of grains as the square before." The emperor agreed, amazed that
the man had asked for such a small reward. After a week, his
treasurer came back and informed him that the reward would add up
to an astronomical sum, far greater than all the rice that could
conceivably be produced in many centuries!
Write a for-loop to compute this astronomical sum. It should output
the total number of rice grains. Note the amount of rice in each
square is a power of 2.
Code:
s = 0
#declared a variable named "s" to calculate "sum"
g = 1
#declared a variable named "g" to calculate count of "grains"
for sqr in range(1, 65):
#using for loop iterating in the range of 1 to 65
s+= g
#updating the sum for every box
g *= 2
#squaring the amount of grains
print("By Square {} The sum = {}
grains".format(sqr,s))
#printing the output square wise
print("\nTherefore the total grains for 64 squares is {}
grains".format(s))
#printing the final sum
Explanation:
The above python code will print the grains as sum of square of each square
and finally print the sum of the total grains by the square 64
Screenshot of the code:
Screenshots of Output:
Write a python code for the following: There is a famous legend about the origin of...
5: Legend has it that the game of chess was invented for the amusement of a Persian shah - or an Indian maharajah, or a Chinese emperor - who became so enthusiastic that he wanted to reward the inventor, who desired only one grain of wheat on the first square of the chessboard, two grains on the second square, four on the third, and so on, doubling the number of grains for each successive square on the 64-square chessboard. (From...