| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | 230908T062604Z | 138 Aspe | |
| 175 | Wolfram Language Mathematica | 211224T032104Z | alephalp |
| nan | 211224T022214Z | caird co |
Trivial builtin answers
Python + sympy, 46 bytes
Saved 32 bytes thanks to @Stef
46 bytes version. Attempt This Online!
lambda y:degree(minpoly(y));from sympy import*
78 bytes version. Attempt This Online!
from sympy import*
x=symbols('x')
f=lambda y:degree(minimal_polynomial(y,x),x)
Wolfram Language (Mathematica), 175 bytes
Cases[FactorList@g@#,{y_,_}/;Simplify[y/.x->#/.s->D]==0:>y~Exponent~x]&
g@n_=x-n
g[p_~s@o_~q_]:=Resultant[g@q,g@p/.x-><|Plus->z-x,Times->z/x,Divide->z*x,Surd->z^q|>@o,x]/.z->x
Takes an expression as input, where:
- \$p+q\$ is represented by
s[Plus][p, q]. - \$p\ q\$ is represented by
s[Times][p, q]. - \$p/q\$ is represented by
s[Divide][p, q]. - \$\sqrt[n]p\$ is represented by
s[Surd][p, n]. - There is no special form for \$p-q\$, because \$p-q\$ is just \$p+(-1)\times q\$.
For example, \$\sqrt[5]5+\sqrt[7]3-2\sqrt[2]2\$ is represented by s[Plus][s[Plus][s[Surd][5, 5], s[Surd][3, 7]], s[Times][-2, s[Surd][2, 2]]].
The idea is constructing a (usually not minimal) polynomial using resultants, factoring it, and choosing the correct factor. The last step requires checking whether a expression is equal to zero. I'm not sure if Simplify is powerful enough for all possible inputs, but at least it works for all the testcases.
Trivial builtin answers
Wolfram Language (Mathematica), 33 bytes
#~MinimalPolynomial~x~Exponent~x&
Unsurprisingly, MinimalPolynomial returns the minimal polynomial of its input, and Exponent gets the highest exponent in this polynomial. We use a bit of infix notation (a~f~b is f[a,b]) to save a couple of bytes, and so this translates as
#~MinimalPolynomial~x~Exponent~x&
Exponent[#~MinimalPolynomial~x,x]&
Exponent[MinimalPolynomial[#,x],x]&