Question

Write a python code for the following: There is a famous legend about the origin of...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:


  
  

Add a comment
Know the answer?
Add Answer to:
Write a python code for the following: There is a famous legend about the origin of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5: Legend has it that the game of chess was invented for the amusement of a...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT