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

Jelly, 8? 11 bytes

Ærṛ/ȧÆr,NċAƲƇN+AƲƇ

A monadic Link that accepts a list of polynomialthe coefficients ([...,d[d,c,b,a]) and yields a list of the (one or three) real roots, unless a is zero in which case it yields an empty list.

Try it online!Try it online! Or see the test-suitetest-suite.

...\$8\$ bytes if we can handle a being zero (in which case it works for all polynomials):

Ær,NċAƲƇ

Try it online!

How?

The heavy lifting is done by a built-in...

Ærṛ/ȧÆr,NċAƲƇN+AƲƇ - Link: coefficients, P = [d,b,c,a]
 /          - reduce (P) by:
ṛ           -   right
               -> a
   Ær       - polynomial roots (of P)
  ȧ         - logical AND
               -> [d,c,b,a] or 0
          Ƈ - keep those for which (with 0, keep those of range(0)=[]):
         Ʋ  -   last four links as a monad - f(x):
      N     -     negate (x)
     ,      -     (P) paired with (that)
        A   -     absolute value (of x)
       ċ    -     count occurrences (of that in the pair)
                    -> Truthy (1 or 2*) if x is real, falsey (0) if not
                                  * when x=0

Jelly, 8 bytes

Ær,NċAƲƇ

A monadic Link that accepts a list of polynomial coefficients ([...,d,c,b,a]) and yields the real roots.

Try it online! Or see the test-suite.

How?

The heavy lifting is done by a built-in...

Ær,NċAƲƇ - Link: coefficients, P
Ær       - polynomial roots (of P)
       Ƈ - keep those for which:
      Ʋ  -   last four links as a monad - f(x):
   N     -     negate (x)
  ,      -     (P) paired with (that)
     A   -     absolute value (of x)
    ċ    -     count occurrences (of that in the pair)
                 -> Truthy (1 or 2*) if x is real, falsey (0) if not
                                  * when x=0

Jelly, 8? 11 bytes

ṛ/ȧÆr,N+AƲƇ

A monadic Link that accepts a list of the coefficients ([d,c,b,a]) and yields a list of the (one or three) real roots, unless a is zero in which case it yields an empty list.

Try it online! Or see the test-suite.

...\$8\$ bytes if we can handle a being zero (in which case it works for all polynomials):

Ær,NċAƲƇ

Try it online!

How?

The heavy lifting is done by a built-in...

ṛ/ȧÆr,N+AƲƇ - Link: coefficients, P = [d,b,c,a]
 /          - reduce (P) by:
ṛ           -   right
               -> a
   Ær       - polynomial roots (of P)
  ȧ         - logical AND
               -> [d,c,b,a] or 0
          Ƈ - keep those for which (with 0, keep those of range(0)=[]):
         Ʋ  -   last four links as a monad - f(x):
      N     -     negate (x)
     ,      -     (P) paired with (that)
        A   -     absolute value (of x)
       ċ    -     count occurrences (of that in the pair)
                    -> Truthy (1 or 2*) if x is real, falsey (0) if not
                                  * when x=0
added 70 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Jelly, 8 bytes

Ær,NċAƲƇ

A monadic Link that accepts a list of polynomial coefficients ([...,d,c,b,a]) and yields the real roots.

Try it online! Or see the test-suite.

How?

The heavy lifting is done by a built-in...

Ær,NċAƲƇ - Link: coefficients, P
Ær       - polynomial roots (of P)
       Ƈ - keep those for which:
      Ʋ  -   last four links as a monad - f(x):
   N     -     negate (x)
  ,      -     (P) paired with (that)
     A   -     absolute value (of x)
    ċ    -     count occurrences (of that in the pair)
                 -> Truthy (1 or 2*) if x is real, falsey (0) if not
                                  * when x=0

Jelly, 8 bytes

Ær,NċAƲƇ

A monadic Link that accepts a list of polynomial coefficients ([...,d,c,b,a]) and yields the real roots.

Try it online! Or see the test-suite.

How?

The heavy lifting is done by a built-in...

Ær,NċAƲƇ - Link: coefficients, P
Ær       - polynomial roots (of P)
       Ƈ - keep those for which:
      Ʋ  -   last four links as a monad - f(x):
   N     -     negate (x)
  ,      -     (P) paired with (that)
     A   -     absolute value (of x)
    ċ    -     count occurrences (of that in the pair) -> 1 if x is real, 0 if not

Jelly, 8 bytes

Ær,NċAƲƇ

A monadic Link that accepts a list of polynomial coefficients ([...,d,c,b,a]) and yields the real roots.

Try it online! Or see the test-suite.

How?

The heavy lifting is done by a built-in...

Ær,NċAƲƇ - Link: coefficients, P
Ær       - polynomial roots (of P)
       Ƈ - keep those for which:
      Ʋ  -   last four links as a monad - f(x):
   N     -     negate (x)
  ,      -     (P) paired with (that)
     A   -     absolute value (of x)
    ċ    -     count occurrences (of that in the pair)
                 -> Truthy (1 or 2*) if x is real, falsey (0) if not
                                  * when x=0
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Jelly, 8 bytes

Ær,NċAƲƇ

A monadic Link that accepts a list of polynomial coefficients ([...,d,c,b,a]) and yields the real roots.

Try it online! Or see the test-suite.

How?

The heavy lifting is done by a built-in...

Ær,NċAƲƇ - Link: coefficients, P
Ær       - polynomial roots (of P)
       Ƈ - keep those for which:
      Ʋ  -   last four links as a monad - f(x):
   N     -     negate (x)
  ,      -     (P) paired with (that)
     A   -     absolute value (of x)
    ċ    -     count occurrences (of that in the pair) -> 1 if x is real, 0 if not