[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Solve Car Talk's Fishing for Thirds puzzler in Python

[A simple loop can solve Car Talk's Fishing for Thirds puzzler in Python]

This example solves the "Fishing for thirds" Car Talk puzzler.

Problem Description

Here's the puzzle in a slightly rewritten form.

Three guys go out fishing and they decide to split their catch evenly in three ways. They fish all day and then go to sleep on their boat tied to the dock.

In the middle of the night, the first guy decides to leave. When he looks at their catch, he sees that it isn't divisible by three. He realizes that if he throws one fish overboard, the remaining number of fish will be divisible by three, so he does that. He throws away one fish and takes a third of the remaining fish.

A few hours later, the second guy wakes up and has the same problem: the number of fish isn't divisible by three but it will be if he throws one fish overboard. So he does exactly that. He throws one fish overboard and takes one third of the remaining fish.

The third guy wakes up in the morning and he's late for work. He figures the other two are still sleeping so he decides to sneak out without waking them. Like the others, he faces the same problem: the number of fish isn't divisible by three, but it will be if he throws one fish overboard, so that's what he does. He discards one fish and takes one third of the remaining fish.

The question is: What is the smallest number of fish they could have initially had to make this work?

Solution

One easy way to solve this in Python is to just try different numbers of fish and see if they work. To do that, let's define the following variables.

  • n = The total number of fish
  • x = The number of fish the first guy takes
  • y = The number of fish the second guy takes
  • z = The number of fish the third guy takes

For a given a value of n, the first guy discards one fish leaving n - 1. He then takes one third of those fish, so x = (n - 1) / 3.

The next guy sees x fish, discards one leaving x - 1, and then takes a third of those so y = (x - 1) / 3.

The last guy sees y fish, discards one leaving y - 1, and then takes a third of those so z = (y - 1) / 3.

The simple Python solution is to make variable n loop from 0 to some big number, calculate x, y, and z, and check whether those values are all integers.

Here's my solution.

# The first guy takes x fish, the second takes y, and the third takes z. # Initially there are n fish. for n in range(10000): # See if n works. x = (n - 1) / 3 y = (2 * x - 1) / 3 z = (2 * y - 1) / 3 if z.is_integer() and y.is_integer() and x.is_integer(): print(f'Total of {n} fish: {int(x)} {int(y)} {int(z)}') break else: print('Could not find a solution')

When you run this code, the program spits out the following.

Total of 25 fish: 8 5 3

Conclusion

I'm sure there's a clever way to use number theory to solve this problem, and maybe I'll look into that at some point, but for some problems it's just easier to write a loop and see what happens.

Download the example to experiment with it and to see additional details.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.