Python Tutorial (33) - Example: Sharing a fish among five people

Time: Column:Python views:250

Problem: How Many Fish Were Caught?

Five people (A, B, C, D, and E) went fishing together one night. By early morning, they were all exhausted and decided to sleep. The next day, A woke up first and divided the fish into five equal parts. There was one extra fish, which he threw away. He took his share and went back to sleep.

Later, B woke up, did the same as A—divided the remaining fish into five equal parts, threw away the extra fish, took his share, and went back to sleep.

C, D, and E woke up one after another, each following the same process: dividing the remaining fish into five equal parts, discarding the extra fish, and taking their share.

The question is: what is the minimum number of fish they could have caught?

Example

The following Python code provides a solution to this problem:

def main():
    fish = 1
    while True:
        total, enough = fish, True
        for _ in range(5):
            if (total - 1) % 5 == 0:
                total = (total - 1) // 5 * 4
            else:
                enough = False
                break
        if enough:
            print(f'The total number of fish is {fish}')
            break
        fish += 1

if __name__ == '__main__':
    main()

Output

Running the above code will yield the following result:

The total number of fish is 3121

This means that the minimum number of fish they could have caught is 3,121.