Skip to main content
added 148 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Just a little fiddly when the input defines a constant since we find no roots whereupon finding coefficients would error (this is why J}¹? is there).

Just a little fiddly when the input defines a constant since we find no roots whereupon finding coefficients would error (this is why J}¹? is there).

Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Jelly, 26 bytes

Ærm2ÆṛJ}¹?×AṪ½Ɗ×þ`ŒdṙLƊṚ§⁼

A monadic Link that accepts a non-empty list of coefficients ordered by degree* and yields 1 if the input represents the square of an integer polynomial, or 0 if not.

* i.e. the reverse order to the examples in the question, and \$0\$ being [0] rather than []

Try it online!

How?

Same approach as loopy walt's Python answer:

  • get the polynomial with every other root of the polynomial defined by the input
  • rescale using the square root of the coefficient of the highest degree in the input
  • get the coefficients of the square of that polynomial
  • check if that is equal to the input
Ærm2ÆṛJ}¹?×AṪ½Ɗ×þ`ŒdṙLƊṚ§⁼ - Link: list of coefficients, C
Ær                         - get the roots of the polynomial P defined by C
  m2                       - modulo-2 slice
         ?                 - if...
        ¹                  - ...condition: no-op (falsey if P is just a constant)
    Æṛ                     - ...then: get polynomial coefficients
       }                   - ...else: use the right argument (implicitly C) with:
      J                    -            range of length (in this case that's [1])
                             (call this list NewCoefficients)
              Ɗ            - last three links as a monad - f(C):
           A               -   absolute values
            Ṫ              -   tail (absolute of coefficient of highest degree)
             ½             -   square root
          ×                - (NewCoefficients) multiplied by (that)
                 `         - use as both arguments of:
                þ          -   table of:
               ×           -     multiplication
                      Ɗ    - last three links as a monad - f(X=that)
                  Œd       -   anti-diagonals of X (starts with main, sweeps South-East)
                                 (has the effect of grouping product coefficients by degree)
                     L     -   length of X
                    ṙ      -   rotate (the anti-diagonals) left by (length of X)
                                 (ensures that the groups are sorted by degree descending)
                       Ṛ   - reverse
                        §  - sums (sum each group of like-degree product coefficients)
                         ⁼ - equals C?