擅长:python、mysql、java
<p>根据维基百科,莫普兹金数满足这个循环关系:</p>
<pre><code>M_n = ((2n + 1)/(n + 2)) M_(n-1) + ((3n - 3)/(n+2)) M_(n-2)
</code></pre>
<p>这很容易翻译成代码:</p>
<pre><code>from itertools import count
def mot():
M_n1 = 1
M_n2 = 1
yield 1
yield 1
for n in count(2):
M = ((2*n + 1)/(n + 2))*M_n1 + ((3*n - 3)/(n+2))*M_n2
M = int(M)
yield M
M_n1, M_n2 = M, M_n1
</code></pre>
<p>现在我们可以循环遍历序列中的项,直到数字变得太大而无法存储,或者从列表的前面切几个:</p>
<pre><code>from itertools import islice
print(list(islice(mot(), 10)))
# [1, 1, 2, 4, 9, 21, 51, 127, 323, 835]
</code></pre>