<p>你的任务的前提是不正确的:</p>
<blockquote>
<p>Start with any number. If the number is divisible by 3, divide it by 3. Otherwise, add 2 to the number. Eventually, no matter what number you begin with, this series will run into 1. </p>
</blockquote>
<pre><code>number = 4
4 + 2 = 6 # 4 is not divisible by 3, add 2
6 / 3 = 2 # 6 is divisible by 3 -> 2
2 + 2 = 4 # 2 is not divisible by 3, add 2
4 + 2 = 6 # 4 is not divisible by 3, add 2 > cycle - no solution.
</code></pre>
<p>如果有解决方案,就会发现:</p>
<pre><code>def joyner(num):
count = 0
seen = set()
while num != 1:
if num in seen:
return None
else:
seen.add(num)
if num % 3 == 0:
num = num // 3
else:
num += 2
count += 1
return count
</code></pre>
<p>测试它:</p>
<pre><code>i = 0
for k,c in d.items():
if c is None:
continue
print(k,":",c, end=" ")
i += 1
if i % 6 == 0:
print()
i = 0
</code></pre>
<p>从1到99的溶液输出:</p>
<pre><code> 1 : 0 3 : 1 5 : 4 7 : 3 9 : 2 11 : 7
13 : 6 15 : 5 17 : 6 19 : 5 21 : 4 23 : 5
25 : 4 27 : 3 29 : 10 31 : 9 33 : 8 35 : 9
37 : 8 39 : 7 41 : 8 43 : 7 45 : 6 47 : 9
49 : 8 51 : 7 53 : 8 55 : 7 57 : 6 59 : 7
61 : 6 63 : 5 65 : 8 67 : 7 69 : 6 71 : 7
73 : 6 75 : 5 77 : 6 79 : 5 81 : 4 83 : 13
85 : 12 87 : 11 89 : 12 91 : 11 93 : 10 95 : 11
97 : 10 99 : 9
</code></pre>
<p>不可行:</p>
<pre><code>[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,
42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78,
80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
</code></pre>
<p>你的算法只适用于奇数。你知道吗</p>
<blockquote>
<p>We tested your code with num = 12. We expected joyner to return the int 3. However, it returned the int 1.</p>
</blockquote>
<p>根本不起作用</p>