Python Practice Example 21
Problem:Monkey eating peaches problem: On the first day, the monkey picked a number of peaches. It immediately ate half of them, and not being satisfied, ate one more. The next morning, it ate half of the remaining peaches, and one more. From then on, every morning it ate half of what was left from the previous day plus one. On the 10th morning, when it wanted to eat again, there was only one peach left. Find how many peaches were picked on the first day.
Program analysis:Use reverse thinking and infer from back to front.
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:
1534Other extensions
Python 100 Examples