Given an integral polynomial \$p\$, determine if \$p\$ is a square of another integral polynomial.
An integral polynomial is a polynomial with only integers as coefficients.
For example, \$x^2+2x+1\$ should gives truthy, because \$x^2+2x+1 = (x+1)^2\$.
On the other hand, \$2x^2+4x+2\$ should gives falsy: \$2x^2+4x+2 = (\sqrt{2}x+\sqrt{2})^2\$. but \$\sqrt{2}x+\sqrt{2}\$ is not an integral polynomial.
Input
A polynomial, in any reasonable format. For example, the polynomial \$x^4-4x^3+5x^2-2x\$ may be represented as:
- a list of coefficients, in descending order:
[1,-4,5,-2,0]
; - a list of coefficients, in ascending order:
[0,-2,5,-4,1]
; - a list of pairs of
(coefficient, degree)
, in any order:[(1,4),(-4,3),(5,2),(-2,1),(0,0)]
; - a map with degrees as keys and coefficient as values:
{4:1,3:-4,2:5,1:-2,0:0}
; - a string representation of the polynomial, with a chosen variable, say
x
:"x^4-4*x^3+5*x^2-2*x"
; - a built-in polynomial object, e.g.,
x^4-4*x^3+5*x^2-2*x
in PARI/GP.
Output
A value representing whether the polynomial is a square. You can choose to
- output truthy/falsy using your language's convention (swapping is allowed), or
- use two distinct, fixed values to represent true (affirmative) or false (negative) respectively.
This is code-golf, so the shortest code in bytes wins.
Testcases
Here I use coefficient lists in descending order:
Truthy
[]
[25]
[1,2,1]
[1,2,1,0,0]
[1,4,0,-8,4]
[4,28,37,-42,9]
[4,0,-4,4,1,-2,1]
[1,-12,60,-160,240,-192,64]
Falsy
[-1]
[24,1]
[1,111]
[2,4,2]
[1,2,1,0]
[1,3,3,1]
[1,-4,5,-2,0]
[4,0,-4,4,1,2,1]
[1,-9,30,-45,30,-9,1]
[0]
instead of[]
? \$\endgroup\$