-2
\$\begingroup\$

The coefficients of a perfect square polynomial can be calculated by the formula \$(ax)^2 + 2abx + b^2\$, where both a and b are integers. The objective of this challenge is to create a program that not only can find if an input trinomial is a perfect square, but also find its square root binomial. The input trinomial will be written in this format:

1 2 1

which symbolizes the perfect square number \$x^2 + 2x+ 1\$, since all 3 input numbers represent coefficients of the trinomial. The outputs must be readable and understandable. To count as a perfect square in this challenge, a trinomial must have \$a\$ and \$b\$ as real integer numbers. No fractions, decimals, irrational numbers or imaginary/complex numbers allowed in the final binomial. Make a program that accomplishes this, and since this is , the shortest code in bytes wins.

\$\endgroup\$
12
  • 2
    \$\begingroup\$ What can we do for output where it's not a perfect square? Some more test cases would be good. \$\endgroup\$ – xnor Sep 20 '20 at 0:16
  • 2
    \$\begingroup\$ Related to Luis Mendo's question on number types, what if the input is 2 4 2? Its square root is \$ \sqrt 2 x + \sqrt 2 \$. Does it count as a perfect square? \$\endgroup\$ – xnor Sep 20 '20 at 0:17
  • 2
    \$\begingroup\$ @NipDip Thanks for the clarifications. Please edit them into the question. \$\endgroup\$ – xnor Sep 20 '20 at 4:17
  • 2
    \$\begingroup\$ Please put in more test cases, ideally covering all corner cases that programs might encounter. These might include various coefficients being negative or zero. In particular, can the input have zero quadratic term or even just be a constant? Can it even be all zero? \$\endgroup\$ – xnor Sep 20 '20 at 10:43
  • 3
    \$\begingroup\$ I downvoted because of too restrictive output format and because the challenge text does not match the clarifications in the comments. Let me know if you solve that so I can remove my downvote \$\endgroup\$ – Luis Mendo Sep 20 '20 at 10:43
2
\$\begingroup\$

JavaScript (Node.js), 42 bytes

I would delete this, but I can't because it's been accepted.

(a,b,c)=>[A=Math.sqrt(a),b/A/2,b*b==c*a*4]

Try it online!

Can be called as f(a,b,c). Output is in the form [<factor of x>, <constant>, True/False] (if it's False, then the first 2 values are meaningless).

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.