My immediate approach was to come up with a closed formula for the number of coins produced by the bag. If
We know that
After taking a step back, I tried reducing the problem down to its base form: "If I put in 1 coin, can I find out how many coins will come out?" This ended up seeming like a promising path forward, since I know that the answer must be 2, since it must be greater than 1 by rule (2) and less than 3 by rule (3). So we can start to fill out the table below: the first line allows us to fill in the second line (
Input |
||
---|---|---|
1 | 2 | 3 |
2 | 3 | 6 |
3 | 6 | 9 |
4 | 12 | |
5 | 15 | |
6 | 9 | 18 |
7 | 21 | |
8 | 24 | |
9 | 18 | 27 |
10 | 30 | |
11 | 33 | |
12 | 36 | |
13 | 39 |
We now know that that
Input |
||
---|---|---|
1 | 2 | 3 |
2 | 3 | 6 |
3 | 6 | 9 |
4 | 7 | 12 |
5 | 8 | 15 |
6 | 9 | 18 |
7 | 12 | 21 |
8 | 15 | 24 |
9 | 18 | 27 |
10 | 19 | 30 |
11 | 20 | 33 |
12 | 21 | 36 |
13 | 39 |
To solve 13, we'll need to extend the table further to 15, since we need to set an upper bound for 13 and we know that
Input |
||
---|---|---|
... | ... | ... |
8 | 15 | 24 |
9 | 18 | 27 |
10 | 19 | 30 |
11 | 20 | 33 |
12 | 21 | 36 |
13 | 39 | |
14 | 42 | |
15 | 24 | 45 |
Finally, as before, we know that
Input |
||
---|---|---|
... | ... | ... |
12 | 21 | 36 |
13 | 22 | 39 |
14 | 23 | 42 |
15 | 24 | 45 |
Final answer: The bag will produce 22 coins when you place in 13.
You can find TED-Ed's solution to the problem on YouTube starting at 2:00. Their solution is the same as mine, but with prettier drawings.
I ran through a couple more examples to see if a pattern emerged or if a closed formula was even possible. Could there be some larger number for which we can't resolve to a unique number?
Looking at the ratio between
%matplotlib inline
import matplotlib.pyplot as plt
%config InlineBackend.figure_format="retina"
data = [
2,3,6,7,8,9,12,15,18,19,20,21,22,23,24,25,26,27,30,33,36,39,42,45,48,51,54,
55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
80,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,
138,141,144,147,150,153,156,159,162,163,164,165,166,167,168,169,170,171,
172,173,174,175,176,177,178,179,180,181,182,183,
]
x_range = range(1, len(data) + 1)
plt.plot(x_range,data)
plt.xticks(x_range[::10])
plt.title("Gold coins produced")
plt.xlabel("Input $n$")
plt.ylabel("Output $f(n)$")
plt.show()
An even clearer pattern emerges when looking at successive differences (see table.csv again): the differences are always either 1 or 3, as the above graph implies. Furthermore, the differences are 1 when the ratio of
Finally, we note that the values of
Putting all of this together, we can obtain a closed formula for
Final answer:
Given an input number of coins
We can also convert this to a Python function for fun (and readability?):
import math
def is_power(number: int, base: int) -> bool:
if number == 1:
return True
while number % base == 0:
number //= base
return number == 1
def coins_produced(n: int) -> int:
if is_power(n, 3):
return 2 * n
if is_power(n / 2, 3):
return int(3 * n / 2)
m = math.floor(math.log(n, 3))
if 2 * 3**m < n:
return 3 * n - 3 ** (m + 1)
return 3**m + n