Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 21

Python Exercise Example 21

Python 100 Examples Python 100 Examples

**Question:** The monkey and peaches problem: A monkey picks some peaches on the first day, immediately eats half of them, and not satisfied, eats one more. The next morning it eats half of the remaining peaches and one more. Every morning after that it eats half plus one of what remained from the previous day. On the morning of the 10th day, when it wants to eat again, only one peach is left. How many peaches did it pick on the first day?

**Analysis:** Use reverse thinking and infer from the end back to the beginning.

Program source code:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

x2

=

1

for

day

in

range

(

9

,

0

,-

1

)

:

x1

=

(

x2

1

)

2

x2

=

x1

print

(

x1

)

The output of the above example is:


1534

Python 100 Examples Python 100 Examples