This assignment gives you more practice solving problems and using Python's selection and repetition commands. Your first task has a special place in my heart: I am a chessplayer, and the first program my older daughter and I wrote together was to solve this problem, from a children's book.
I encourage you to personalize your program and make it your own. Feel free to use all the things you are learning in class and the text (Chapters 1-3) to make your program as good as you would like. However, be sure not to change the input/output specification, especially the number and order of the data values your program reads from the user. As always, be sure to meet the course programming standards.
A popular children's book tells a variation of a well-known fable. A wise man provides service to a powerful king, who offers payment in the form of luxurious gifts. The wise man declines, instead making a simple request to help feed the king's people On the first day, the king will place one grain of rice on the first square of a chessboard. On the second day, the king will place two grains of rice on the next square. On the third day, he will will place four grains of rice on the next, and so on, doubling the number of grains placed on the next square, until the chessboard is full.
The king scoffs at the man for turning down treasure and taking mere grains of rice instead. But the wise man is wise, indeed, for the request soon proves to be an impossible challenge for the royal granary to satisfy. As one book review says, "An amusing scenario unfolds as the amount of rice multiplies daily, causing great curiosity among the villagers and embarrassment to the prideful king, who learns a valuable lesson".
Notice that one the first day, the king places 1 = 20 grains of rice on the board. On the second day, he places 2 = 21 grains of rice on the board. On the third, he places 4 = 22 grains of rice on the board. There are sixty-four squares on a chessboard, so this continues until the sixty-fourth day, when the king places 263 grains of rice on the board. Talk about compound interest!
Write a Python program to answer the questions:
You may assume that a single grain of rice weighs approximately 50 mg and has a volume of 0.05 cm**3, and that we spread the rice evenly over whatever region the user chooses.
You must use a while or for loop to calculate the sum of the grains of rice on the chessboard. Do not use a formula for the caluculation, even if you know it by heart. Practicing writing a loop!
Name your program grains_of_rice.py.
This task also involves a historical story about calculation, this time an alternative way to multiply. This algorithm is attributed variously to different ancient cultures, including the Egyptians and (as the name implies) Russian peasants.
The idea is straightforward: the product of two integers m and n is equal to the product of 2m and n/2. If we keep multiplying m by 2 and dividing n by 2, eventually n will become 1, and m becomes the product of the original two numbers. (Try it with, say, m=7 and n=8.)
What happens if n becomes odd before it reaches 1? We lose an m. So we have to remember it in a running total. So the algorithm becomes:
When we divide n by 2, we use integer division. In Python, we thus use the // operator.
For example, suppose we want to know the value of 26*42. The algorithm proceeds as follows:
|m||n||comment||value of running sum|
|26||42||n is even||0|
|52||21||add 52 before * and //||52|
|104||10||n is even||52|
|208||5||add 208 before * and //||260|
|416||2||n is even||260|
|832||1||add 832 before * and //||1092|
|1664||0||n is zero||1092|
n is 0, so we stop. We ignore the value in the m column. The product 26 * 42 is the value of the running sum, 1092.
Write a Python program to implement this algorithm:
You must use a while or for loop to implement the repeated doubling and halving. A simple if statement is all you need to decide when to update the running sum.
Name your program russian_peasant.py.
Start a fresh Python shell. Run each of your programs in the shell on three to five examples that demonstrate the correctness of your program. Save your shell to a text file named interactions.py.
For Task 1, use the state of Iowa for one of your examples.
By the due date and time, submit:
Use the on-line submission system.
Make sure that your program meets the course programming standards.