如何计算两个相互依赖的序列?

How to calculate two sequences depending on each other?

我正在尝试在下面编写序列,但它不起作用。你能帮帮我吗

import math
def un(n_terms):
  def vn(n_terms):
    p = 0
    sum_un = 0
    while n_terms>=1:
      while p<=n_terms:
        sum_un = sum_un + (1//math.factorial(p))
        p = p + 1
    un(n_terms) = sum_un
    vn(n_terms) = un(n_terms) + (1//(n_terms * math.factorial(n_terms)))
  print(vn(10))
print(un(10))

您可以像这样简化您的函数:

import math

def u(n):
    return sum(1/math.factorial(p) for p in range(n+1))

def v(n):
    return u(n) + 1/(n * math.factorial(n))

然后你可以像这样使用它们:

print(u(10)) # 2.7182818011463845
print(v(10)) # 2.7182818287037036