How Many Peaches Did the Monkeys Start With? A Math & Code Puzzle
This article presents a classic monkey‑and‑peach puzzle, derives the minimal initial peach count using mathematical reasoning, and then demonstrates a brute‑force Python program that searches for the solution, illustrating how loops and modular checks solve the problem.
Story Origin
One day in a forest a bag of peaches appears, and five monkeys arrive.
Mathematical Solution
Assume the bag initially contains x peaches. Each monkey discards one peach, then takes one fifth of the remaining peaches, leaving 4/5·(x‑1). After the first monkey the remaining amount is y = 4/5·(x‑1). Repeating the same operation five times gives the recurrence y = (4/5)^5·(x‑5) + 4·(1‑(4/5)^5). Because both x and y must be positive integers, x+4 must be a multiple of 5^5. The smallest solution is x = 5^5‑4 = 3121, leaving 1020 peaches after the fifth monkey.
Programmatic Solution
The same problem can be solved by a brute‑force search in Python. The algorithm iterates over candidate initial counts, checks that peach % 5 == 1 holds for five consecutive steps, and updates the count with peach = peach // 5 * 4 each time.
def find_peach():
i = 1
peach = 1
count = 1
while i <= 5:
if peach % 5 == 1:
peach = peach // 5 * 4
i += 1
else:
i = 1
count += 1
peach = count
return countThe function returns 3121, confirming the mathematical result.
Takeaways
Brute‑force enumeration is often sufficient for small‑scale combinatorial problems, and understanding the underlying mathematical recurrence helps to verify the result.
NiuNiu MaTe
Joined Tencent (nicknamed "Goose Factory") through campus recruitment at a second‑tier university. Career path: Tencent → foreign firm → ByteDance → Tencent. Started as an interviewer at the foreign firm and hopes to help others.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
