-1

您好,谁能帮我写一个函数,计算给定 x 和正整数 n 的 1+x+x^2+...+x^n 并用它来计算 (1+x+x^2+.. .+x^10)(1+x^2+x^4+...+x^10) 对于 x=100?

4

6 回答 6

1
def myfunc(x, n, step):
  if n > 0:
    return x**n + myfunc(x, n - step, step)
  return 1

myfunc(100, 10, 1) * myfunc(100, 10, 2)
于 2010-12-17T03:49:47.720 回答
1

您可以使用它来计算1+x+x^2+...+x^n

lambda x, n: sum([x**y for y in range(0, n + 1)])

使用逻辑计算第二个函数。

于 2010-12-17T03:50:32.590 回答
1

由于您在其上放置了 Sage 标记,因此这是在 Sage 中执行此操作的一种有趣方式。

sage: R.<x> = PowerSeriesRing(ZZ)

将 R 定义为以 x 为变量的幂级数。ZZ 意味着我们使用整数作为系数。现在,让我们看看我们可以用它做什么:

sage: R([1, 2])              # the array inside contains the coefficients 
1 + 2*x                      # for each element of the series
sage: R([1]*11)              # this gives us the first power series
1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8 + x^9 + x^10
sage: R([1,0]*5 + [1])       # and here is our second one
1 + x^2 + x^4 + x^6 + x^8 + x^10
sage: R([1]*11).(5)          # we can evaluate these for various x values
12207031
sage: R([1]*11).subs(x=5)    # an alternate way to evaluate
12207031
sage: f = R([1]*11)*R([1,0]*5+[1])  # this constructs the desired function
sage: f(100)                   # we can evaluate it at any value

无论如何,希望您现在了解如何在 Sage 中执行此操作。我自己对 Sage 很陌生,但到目前为止我真的很喜欢它。

于 2010-12-17T07:57:42.367 回答
0

对于你的第一个问题,

x=2;(给定)

n=10;(给定)

检查你自己这些价值观是否是积极的,是否想要

结果=1;

for(a=2;a<=n;a++)

{

结果+=x^a;

}

于 2010-12-17T03:48:07.903 回答
0

我认为这是您正在寻找的功能。

def f(x, n):
    answer = 0
    for y in range(n + 1):
        answer += x ** n
    return answer

我不太明白第二部分。

于 2010-12-17T03:48:21.370 回答
0
function series($x, $n) {

    $answer = 1;           

    for($i = $n; $i > 0; $i--) {

         $answer += pow($x, $i);

    }

        return $answer;
}

series(100, 10) * series(100, 10)
于 2010-12-17T04:09:03.710 回答