Last active
September 9, 2019 00:33
-
-
Save TomFaulkner/eb57a5f468b56abb8241e3fe27e215e4 to your computer and use it in GitHub Desktop.
Car Talk - Going Fishing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"After getting the book [\"Math Adventures with Python\"](https://nostarch.com/mathadventures) I've been more interested in solving math problems in Python and how one might go about it at a junior high or high school level. I teach neither Math or Programming, but I am a programmer. Today I heard the [riddle on a Car Talk re-run](https://www.cartalk.com/puzzler/gone-fishing) and decided to solve it using Python." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# The Riddle\n", | |
"RAY: Three guys go out fishing. They decide in advance that whatever they catch,\n", | |
"they're going to divvy it up equally.\n", | |
"\n", | |
"So, they finish fishing for the day. They pull back into port, and they're going\n", | |
"to sleep on the boat overnight. They're going to get up in the morning, divvy up\n", | |
"the fish, and go home. In the middle of the night, however, one of the guys has\n", | |
"a severe hemorrhoidal flare-up, and he's got to get to the drugstore right away\n", | |
"to buy some stuff.\n", | |
"\n", | |
"So, he goes to take his third of the fish, and he notices that the number that they\n", | |
"caught is not divisible by three, unless he throws one of the fish overboard. So,\n", | |
"he throws one of the fish overboard, takes his third and leaves. A few hours later,\n", | |
"in the middle of the night, another guy wakes up with horrible stomach pains. He’s\n", | |
"gotta have the Kaopectate. So he goes to take his third of the fish, and he notices,\n", | |
"interestingly, the same thing -- he can't take a third unless he throws one fish\n", | |
"overboard. He throws one fish overboard, takes his third, and goes home.\n", | |
"\n", | |
"Third guy gets up in the morning and figures the other guys are still sleeping. So\n", | |
"he figures, “I'll just take my third, and I'll go. When they wake up, they can take\n", | |
"their third.” However, he realizes that he can't take a third. It's not divisible by\n", | |
"three.\n", | |
"\n", | |
"He throws one fish overboard, takes his third, and leaves.\n", | |
"\n", | |
"Question: What is the smallest number of fish by which this little scenario could\n", | |
"have taken place?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Starting 25\n", | |
"First: 8\n", | |
"Second: 5\n", | |
"Third: 3\n", | |
"Rotting: 6\n" | |
] | |
} | |
], | |
"source": [ | |
"first, second, third = 0, 0, 0\n", | |
"for fish in range(300):\n", | |
" fish_remaining = fish\n", | |
"\n", | |
" if (fish_remaining - 1) % 3:\n", | |
" continue\n", | |
" first = (fish_remaining - 1) // 3\n", | |
" fish_remaining -= 1 + first\n", | |
"\n", | |
" if (fish_remaining - 1) % 3:\n", | |
" continue\n", | |
" second = (fish_remaining - 1) // 3\n", | |
" fish_remaining -= 1 + second\n", | |
"\n", | |
" if (fish_remaining - 1) % 3:\n", | |
" continue\n", | |
" third = (fish_remaining - 1) // 3\n", | |
" fish_remaining -= 1 + third\n", | |
" break\n", | |
"\n", | |
"print(f'Starting {fish}\\nFirst: {first}\\nSecond: {second}\\nThird: {third}\\nRotting: {fish_remaining}')\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Explanation\n", | |
"This demonstrates a simple `for` loop with `continue` and `break` along with conditionals (`if`) and the modulus operator to `continue` iteration when it is proven that the guess is incorrect without doing unnecessary checks. Finally, once the first possible answer is proven the loop breaks rather than iterating further.\n", | |
"\n", | |
"Is it better than doing this as an algebraic equation? I don't know, but I think it presents an opportunity for students to think about the logic in another manner and to practice their problem solving skills." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Answer \n", | |
"\n", | |
"RAY: You could do it simply by trial and error. Just keep trying numbers.\n", | |
"\n", | |
"Or you can do a very simple equation: Y=2/3(X-1), so that Y is what you end up with,\n", | |
"X is what you start with, and you keep plugging in numbers for X until you can solve\n", | |
"that equation three times without running into non-integers. That doesn't happen until\n", | |
"you get to 25.\n", | |
"\n", | |
"They caught 25 fish.\n", | |
"\n", | |
"The first guy tosses one overboard because you can't divide 25 by three. That's 24.\n", | |
"He takes his third, which is eight, that leaves 16 fish. The next guy comes; you can't divide\n", | |
"16 by three. Throws one overboard. That's 15. He takes five. That leaves 10 fish.\n", | |
"The third guy shows up in the morning, there's 10 fish. He thinks he's dividing it again\n", | |
"into threes. He can't divide it by three, he throws one fish overboard, takes his three,\n", | |
"leaving six fish on the deck to rot." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment