31
\$\begingroup\$

While bored in high-school (when I was half my current age...), I found that \$f(x) = x^{x^{-1}}\$ had some interesting properties, including e.g. that the maximum \$f\$ for \$0 ≤ x\$ is \$f(e)\$, and that the binding energy per nucleon of an isotope can be approximated as \$6 × f(x ÷ 21)\$...

Anyway, write the shortest function or program that calculates the xth root of x for any number in your language's domain.

Examples cases

For all languages

     -1   >       -1
   ¯0.2   >    -3125
   ¯0.5   >        4
    0.5   >     0.25
      1   >        1
      2   >    1.414
      e   >    1.444 
      3   >    1.442
    100   >    1.047
  10000   >    1.001

For languages that handle complex numbers

   -2   >        -0.7071i
    i   >            4.81         
   2i   >    2.063-0.745i
 1+2i   >   1.820-0.1834i
 2+2i   >   1.575-0.1003i

For languages that handle infinities

-1/∞   >   0    (or ∞ or ̃∞)
   0   >   0    (or 1 or ∞)
 1/∞   >   0
   ∞   >   1
  -∞   >   1

For languages that handle both infinities and complex numbers

 -∞-2i   >   1      (or ̃∞)

̃∞ denotes directed infinity.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Here is a Wolfram Alpha plot for positive real x. If you omit the x limits in the query, Wolfram Alpha will include negative values of x where the function value depends on a choice of "branch" for the complex logarithm (or for a similar complex function). \$\endgroup\$ Commented Feb 24, 2016 at 14:36
  • \$\begingroup\$ What about for languages that do not handle power of decimals? \$\endgroup\$
    – Leaky Nun
    Commented Mar 31, 2016 at 3:24
  • 1
    \$\begingroup\$ @KennyLau Feel free to post with a note that says so, especially if the algorithm would work, had the language supported it. \$\endgroup\$
    – Adám
    Commented Mar 31, 2016 at 5:37

65 Answers 65

1 2
3
1
\$\begingroup\$

Pyth, 1 byte

@

Pyth takes implicit input, so @ is really equivalent to @QQ, where Q is the evaluated input.

Try it online!

\$\endgroup\$
1
1
\$\begingroup\$

Vyxal, 2 bytes

Ėe

Try it Online!

The es are coming!

 e # x to the power of
Ė  # 1/x
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 9 bytes

~x=x^x^-1

Try it online!

This solution handles both complex numbers and infinities.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 2 bytes

zm

Try it online!

zm  # full program
    # implicit input...
 m  # to the power of...
z   # 1 divided by...
    # implicit input
    # implicit output
\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 2 bytes

Ị*

Attempt This Online!

calculates the reciprocal, and * does exponentiation.

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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