首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多项式根的多重性

多项式根的多重性
EN

Code Golf用户
提问于 2022-10-16 13:35:08
回答 13查看 1.3K关注 0票数 17

p(x)是多项式。我们说a是多重k of p(x)的根,如果存在另一个多项式s(x),例如p(x)=s(x)(x-a)^ks(a)\ne0

例如,多项式p(x)=x^3+2x^2-7x+4=(x+4)(x-1)^21-4作为根。1是多重性2的根。-4是多重性1的根。

任务

给出一个非零多项式p(x)和它的根a,求出a的多重性。

p(x)的系数都是整数。a也是一个整数。

你可以采取任何合理的形式多项式。例如,多项式x^4-4x^3+5x^2-2x可以表示为:

  • 按降序排列的系数列表:[1,-4,5,-2,0]
  • 按升序排列的系数列表:[0,-2,5,-4,1]
  • 多项式的字符串表示,具有选定的变量,例如,x"x^4-4*x^3+5*x^2-2*x"
  • 内建多项式对象,如PARI/GP中的x^4-4*x^3+5*x^2-2*x

当你把输入作为一个系数的列表时,你可以假设前导系数(第一个降序)是非零的。

这是密码-高尔夫,所以以字节为单位的最短代码获胜。

测试案例

在这里,我按降序使用系数列表:

代码语言:javascript
复制
[1,2,-7,4], 1 -> 2
[1,2,-7,4], -4 -> 1
[1,-4,5,-2,0], 0 -> 1
[1,-4,5,-2,0], 1 -> 2
[1,-4,5,-2,0], 2 -> 1
[4,0,-4,4,1,-2,1], -1 -> 2
[1,-12,60,-160,240,-192,64,0], 2 -> 6
EN

回答 13

Code Golf用户

发布于 2022-10-16 13:47:52

果冻,3字节

代码语言:javascript
复制
Ærċ

在网上试试!

将输入作为按升序排列的系数列表。TIO上的页脚反转每个测试用例以适应这种情况。

是如何工作的

代码语言:javascript
复制
Ærċ - Main link. Takes a polynomial P on the left, and a root r on the right
Ær  - Calculate the roots of P, with repeats
  ċ - Count the number of times r appears in the list of roots
票数 6
EN

Code Golf用户

发布于 2022-10-16 13:56:48

JavaScript (ES7),62字节

期望(root)(polynomial),其中多项式是按升序排列的系数列表。

代码语言:javascript
复制
(r,s=0)=>g=p=>s?-1:1+g(p.map((c,i)=>(s+=c*r**i,c*i)).slice(1))

在网上试试!

怎么做?

这简单地递归地计算多项式的连续导数:

P_{k+1}(x)=\frac{d}{dx}P_k(x)

直到P_k(r)\neq 0并返回迭代次数。

评论

代码语言:javascript
复制
(                   // outer function taking:
  r,                //   the root r
  s = 0             //   the sum s of the polynomial evaluation
) =>                //
g = p =>            // inner recursive function taking the polynomial p[]
s ?                 // if s is not equal to 0:
  -1                //   stop the recursion and decrement the final result
:                   // else:
  1 +               //   increment the final result
  g(                //   do a recursive call with the derivative of p[]:
    p.map((c, i) => //     for each coefficient c at position i in p[]:
      (             //
        s +=        //       add to s:
          c *       //         the coefficient multiplied by
          r ** i,   //         the root raised to the power of i
        c * i       //       set the new coefficient to c * i
      )             //
    ).slice(1)      //     end of map(); remove the leading term
  )                 //   end of recursive call

59字节

一个没有slice()的版本,由@tsh建议。

代码语言:javascript
复制
(r,s=k=0)=>g=p=>s?~k:g(p.map(c=>(s+=0|c*r**i,c*i++),i=k--))

在网上试试!

票数 6
EN

Code Golf用户

发布于 2022-10-16 19:16:29

Python3.8 (预发行版),65字节

代码语言:javascript
复制
f=lambda p,r,c=0:(q:=[c:=b+c*r for b in p])!=c==0and-~f(q[:-1],r)

在网上试试!

除以线性因子X-根,如果余数为零,则递归.

票数 4
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/253294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档