Some languages do not have native ways of representing polynomials, others do. Many times a list of coefficients (in one or the other order) works well. In order to avoid explicitly specifying this over and over again in all challenge, I'd like add that to the tag info.

Please add your suggestions for default representations.

  • 4
    \$\begingroup\$ I'd avoid declaring a default format that needs to be looked up in the tag wiki or on meta. It should be clear from the challenge how I/O can be formatted. That said, having some guidance for the challenge author on what I/O formats to allow would certainly be helpful, much like the recent question on what outputs decision problems should allow. \$\endgroup\$ May 14, 2017 at 17:09
  • \$\begingroup\$ I'm usually for defaults, but I think polynomials are something challenge authors should specify the format for. Our defaults are generally for common programmatic elements (inputs, strings, graphics) to be clarified and standardized across languages. But polynomials are something most languages don't have a existing concept for, so a default would really be making a definition from scratch. \$\endgroup\$
    – xnor
    May 14, 2017 at 20:22
  • \$\begingroup\$ @xnor I disagree, many languages (certainly algebraic or numerical langauges) do have built in polynomial types or at least packages that support them. \$\endgroup\$
    – flawr
    May 14, 2017 at 21:10
  • \$\begingroup\$ @MartinEnder I think there are just a few accepted ways to represent a polynomial anyway, and I think (similarly as in code-golf or string or popularity-contest or quine) to define a default would give us a base to avoid repeating the definition over and over again, as we also do not explicitly say that we use characters to measure code length by default. \$\endgroup\$
    – flawr
    May 14, 2017 at 21:22

5 Answers 5


List of coefficients

A list of coefficients

  • in ascending order [a(0),a(1),a(2),...,a(n)] OR
  • in descending order [a(n),a(n-1),...,a(0)]

If the degree is known, it may be zero padded as



  • \$\begingroup\$ And this 3*x^2*y+4*z; is it not a polynomial? \$\endgroup\$
    – user58988
    May 16, 2017 at 5:19
  • \$\begingroup\$ Multivariable polynomials can be writen as polynomial in one variable with polynomials of the remaining variable as coefficients. \$\endgroup\$
    – flawr
    May 16, 2017 at 18:24

Default representation in [language]

Many langauges, especially ones that include a CAS have native ways of representing polynomials.


A function that takes \$k\$ as input and outputs the coefficient of \$x^k\$

For example, \$24 x^4 + 96 x^3 + 72 x^2 + 16 x + 1\$ can be represented as

f(k) = 24 if k = 4
     = 96 if k = 3
     = 72 if k = 2
     = 16 if k = 1
     = 1  if k = 0
     = 0  otherwise

This is different from the "function representation" in @xnor's answer.

If the challenge requires outputting a polynomial, we can also take \$k\$ as an additional input and output the coefficient of \$x^k\$.

For multivariable polynomials, we can take a list of inputs \$[k_1, k_2, \ldots, k_n]\$ and output the coefficient of \$x_1^{k_1} x_2^{k_2} \cdots x_n^{k_n}\$.


The function and k

The pure function doesn't give an upperbound of k so it needs extra provided

Seems to trivialize some question but anyway list here


Beware function representations

Be careful about allowing a polynomial to be represented by a function that computes it, in languages where functions are objects. For example, in Python the polynomial 3*x^2+2 could be represented by the function object lambda x:3*x*x+2.

Take the challenge to compose polynomials. One could submit

Haskell, 3 bytes


where . is Haskell's function composition operator: if f and g are functions, then f.g is a function that takes x to f(g(x)). This would cut off the possibility for a much more interesting answer in the coefficient list representation.

Even a wordy language like Python could do better with

lambda f,g:lambda x:f(g(x))
  • \$\begingroup\$ I would also vote agains considering these functions as polynomials, because once they are defined, there is usually no way to retrieve the polynomial itself. \$\endgroup\$
    – flawr
    May 14, 2017 at 21:13
  • \$\begingroup\$ Same goes for expressions representing the polynomial in languages with symbolic names, e.g. 3x^2+2 is a valid Mathematica expression that will remain unevaluated (unless it can be simplified). While not a function, it can be manipulated and evaluated quite easily. That said, most Mathematica answers would probably start by converting to this format first, so I'm not sure it's a bad thing to allow the expression right away. \$\endgroup\$ May 15, 2017 at 9:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .