Challenges

+2
−0

Challenge

Given the roots of a polynomial (that is, the $x$ values where the polynomial evaluates to zero), as an array of real numbers, return the polynomial's coefficients.

That is, given real roots $r_1, r_2, \cdots , r_n$, find the coefficients of the expansion of $(x-r_1)(x-r_2)\cdots(x-r_n)$, or any non-zero scalar multiple of it.

You may use either lowest power first or highest power first order for the resulting list of coefficients.

Tests

// Note that any non-zero scalar multiple of these results is valid
[]        -> 
       -> [-1, 1]         // (x - 1) = -1 + 1x
[1, 2]    -> [2, -3, 1]      // (x - 1)(x - 2) = 2 - 3x + 1x^2
[1, 1]    -> [1, -2, 1]      // (x - 1)^2 = 1 -2x + 1x^2
[1, 2, 3] -> [-6, 11, -6, 1] // (x - 1)(x - 2)(x - 3) = -6 + 11x - 6x^2 + x^3

This is code golf, so the shortest answer in bytes wins!

Why does this post require moderator attention?
Why should this post be closed?

+2
−0

Japt-Q, 15 12 bytes

à üÊËx_×*JpE

-3 bytes thanks to @Shaggy

Uses Vieta's.

Try it

Why does this post require moderator attention?

+2
−0

Vyxal, 2 bytes

∆ṙ

Try it Online!

That cheap builtin.

Why does this post require moderator attention?

+0
−0

foldl(\a b->zipWith((-).(*b))(0:a)$a++) Try it online! Uses the "highest power first" convention. Sometimes multiplies the polynomial by$(-1)\$.

Why does this post require moderator attention?

+0
−0

JavaScript (Node.js), 58 bytes

([x,...r],y)=>x?[0,...y=f(r)].map((v,i)=>v-x*(y[i]|0)):

Try it online!

Why does this post require moderator attention?

Featured
Hot Posts
Challenges — "Hello, World!"
Challenges — Coat of Many Colours
Challenges — Cumulative Counts This community is part of the Codidact network. We have other communities too — take a look!