| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | 250621T002647Z | RARE Kpo | |
| 009 | Julia 1.0 | 230615T191640Z | Ashlin H |
| 002 | Thunno 2 | 230615T181950Z | The Thon |
| 002 | Vyxal | 210730T230424Z | emanresu |
| 125 | dc | 170205T053921Z | R. Kap |
| 002 | 05AB1E | 210111T190632Z | Makonede |
| 011 | Bean | 170205T063405Z | Patrick |
| 004 | Quetzalcoatl | 160405T152122Z | NoOneIsH |
| 003 | 05AB1E | 160614T104745Z | Adnan |
| 001 | Pyth | 160407T234101Z | Leaky Nu |
| 010 | Excel | 170920T133004Z | Wernisch |
| 024 | Common Lisp | 170915T220408Z | ceilingc |
| 002 | J | 160223T044529Z | Dennis |
| 018 | SmileBASIC | 170201T220340Z | 12Me21 |
| 009 | QBIC | 160224T163216Z | steenber |
| 023 | tcl | 170115T000904Z | sergiol |
| 007 | Mathematica | 160223T042159Z | Calculat |
| 005 | MATL | 160223T133205Z | Luis Men |
| 034 | Python 3 | 160613T225255Z | george |
| 023 | C | 160223T232523Z | user4594 |
| 014 | Desmos | 160606T185333Z | weatherm |
| 060 | SQL MySQL | 160413T050034Z | user4594 |
| 014 | Mumps | 160408T201101Z | zmerch |
| 003 | Pyth | 160223T035520Z | Doorknob |
| 022 | Racket | 160315T235309Z | Winny |
| 003 | Convex | 160316T024655Z | GamrCorp |
| 010 | Factor | 160316T014852Z | cat |
| 006 | dc | 160405T211741Z | mriklojn |
| 020 | Lua | 160331T032701Z | Leaky Nu |
| 007 | Brachylog | 160316T162941Z | a spaghe |
| 011 | Perl | 160227T080414Z | Sake |
| 009 | PARI/GP | 160223T141945Z | Charles |
| 041 | C# | 160224T161025Z | froggyof |
| 006 | CJam | 160223T063613Z | Dennis |
| 415 | Vitsy | 160227T002934Z | Addison |
| 033 | Scala | 160224T215010Z | Michal |
| 025 | C# | 160224T212112Z | Heiko Ob |
| 013 | Mouse2002 | 160224T173836Z | cat |
| 006 | Pyke commit 29 | 160223T154046Z | Blue |
| 011 | MATLAB | 160224T150142Z | Tom Carp |
| 008 | Awk | 160224T134722Z | Hastur |
| 013 | Gnuplot | 160224T134539Z | Hastur |
| 011 | Haskell | 160223T220503Z | flawr |
| 057 | 𝔼𝕊𝕄𝕚𝕟 | 160223T062355Z | Mama Fun |
| 011 | Javascript ES2016 | 160223T062803Z | Mama Fun |
| 006 | O | 160224T023248Z | user4594 |
| 005 | Jolf | 160224T015854Z | Conor O& |
| 056 | Python 2 | 160223T234908Z | Maltysen |
| 002 | NARS APL | 160223T221854Z | marinus |
| 005 | Milky Way 1.6.5 | 160223T233615Z | Zach Gat |
| 041 | Java | 160223T174617Z | Darrel H |
| 015 | Ruby | 160223T231342Z | Eli Sado |
| 029 | PHP 5.6 | 160223T150013Z | ricdesi |
| 004 | TIBasic | 160223T230106Z | Timtech |
| 017 | R | 160223T053229Z | mnel |
| 048 | C++ | 160223T214848Z | user4594 |
| 018 | Java 8 | 160223T212652Z | GamrCorp |
| 003 | Japt | 160223T210241Z | ETHprodu |
| 018 | JavaScript ES6 | 160223T042621Z | Downgoat |
| 003 | TIBASIC | 160223T161812Z | NinjaBea |
| 005 | Pylons | 160223T160700Z | Morgan T |
| 016 | Codename Dragon | 160223T155812Z | Conor O& |
| 010 | Perl 5 | 160223T072554Z | msh210 |
| 002 | Jelly | 160223T043724Z | Dennis |
| 017 | Python 3 | 160223T042843Z | user4594 |
| 005 | Seriously | 160223T040051Z | user4594 |
awk
jot 50
awk '$2=$1^$1^-1'
1 1
2 1.4142135623731
3 1.44224957030741
4 1.4142135623731
5 1.37972966146121
6 1.34800615459728
7 1.32046924775612
8 1.29683955465101
9 1.27651800700924
10 1.25892541179417
11 1.24357522791237
12 1.23007550557797
13 1.21811404356077
14 1.20744202741841
15 1.19786005826964
16 1.18920711500272
17 1.18135207462555
18 1.17418725309966
19 1.16762348369611
20 1.16158634964154
21 1.1560132810008
22 1.15085130035828
23 1.14605525822348
24 1.14158644063216
25 1.13741146175603
26 1.13350137645871
27 1.12983096390975
28 1.12637814525087
29 1.12312350709179
30 1.1200499091502
31 1.11714215925046
32 1.11438674259589
33 1.11177159504321
34 1.10928591226304
35 1.10691998833269
36 1.1046650785975
37 1.10251328264561
38 1.10045744403373
39 1.09849106402815
40 1.09660822712436
41 1.0948035365073
42 1.09307205793482
43 1.09140927078651
44 1.08981102522989
45 1.08827350462819
46 1.08679319245417
47 1.08536684309049
48 1.08399145599242
49 1.08266425276848
50 1.08138265680029
dc, 125 bytes
15k?ddsk1-A 5^*sw1sn0[A 5^ln+_1^+ln1+dsnlw!<y]syr1<y1lk/*sz[si1[li*li1-dsi0<p]spli0<p]so0dsw[lzlw^lwlox/+lw1+dswA 2^!<b]dsbxp
Unlike the other dc answer, this works for all real \$x\$ greater than or equal to \$1 (\$1 ≤ x$). Accurate to 4-5 places after the decimal.
I would have included a TIO link here, but for some reason this throws a segmentation fault with the version there (dc 1.3) whereas it does not with my local version (dc 1.3.95).
Explanation
As dc does not support raising numbers to non-integer exponents to calculate \$x^\frac1x\$, this takes advantage of the fact that:
$$x^\frac1x = e^\frac{\ln x}x$$
So, to calculate \$\ln(x)\$, this also takes advantage of the fact that:
$$\int \frac1x dx = \ln(x) + c$$
whose definite integral from \$1\$ to \$b = x\$ is numerically-approximated in increments of \$10^{-5}\$ using the following summation formula:
$$\int_1^b \frac1x dx = \sum_{i=1}^{10^{5}(b-1)} \frac 1 {10^5 + i}$$
The resulting sum is then multiplied by \$\frac1x\$ to get \$\frac{\ln(x)}x\$. \$e^{\frac{\ln(x)}x}\$ is then finally calculated using the \$e^x\$ Maclaurin Series to 100 terms as follows:
$$e^x=\sum^{10^2}_{n=0}\frac{x^n}{n!}$$
This results in our relatively accurate output of \$x^\frac1x\$.
05AB1E, 2 bytes
zm
zm # full program
# implicit input...
m # to the power of...
z # 1 divided by...
# implicit input
# implicit output
Bean, 11 bytes
Hexdump:
00000000 26 4c a0 43 95 4c a5 3a 8e 20 43 &L C.L¥:. C
0000000b
Equivalent JavaScript:
A**(1/A)
Implicitly converts first line of input to number as A and implicitly outputs the result of the expression.
Try the demo here.
Try the test suite here.
Quetzalcoatl, 4 bytes
c.I?
Explanation:
c # Input
. # Duplicate
I # Inverse
? # Power
05AB1E, 3 bytes
Dzm
Explanation:
D # Duplicate the input
z # Inverse, 1 ÷ input
m # Power, calculating input ** (1 ÷ input)
Pyth, 1 byte
@
Pyth takes implicit input, so @ is really equivalent to @QQ, where Q is the evaluated input.
Excel, 10 bytes
=A1^(1/A1)
Self explanatory.
J, 2 bytes
^%
How it works
^% Monadic verb. Argument: y
% Inverse; yield 1÷y.
^ Power (hook); compute y ** (1÷y).
SmileBASIC, 18 bytes
INPUT X?POW(X,1/X)
tcl, 23
I post two approaches which occupy the exact same number of bytes.
puts [expr $n**(1./$n)]
—
puts [expr $n**$n**-1.]
Available to test on http://rextester.com/EMW19680
Mathematica, 8 7 4 7 bytes
#^#^-1&
More builtin-only answers, and now even shorter! Nope.
By definition, the next answer should be 13 bytes. (Fibonacci!) The pattern is still broken. :(
MATL, 5 bytes
t-1^^
t % implicit input x, duplicate
-1 % push -1
^ % power (raise x to -1): gives 1/x
^ % power (raise x to 1/x). Implicit display
Python 3: 34
n=complex(input());print(n**(1/n))
Unless I'm missing something this would do nicely. I like that Python normally uses spaces, but this solution doesn't need them.
C, 23 bytes
#define p(a)pow(a,1./a)
This defines a macro function p which evaluates to the ath root of a.
Thanks to Dennis for reminding me that gcc doesn't require math.h to be included.
Thanks to @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ for reminding me that the space after the first ) is not needed.
SQL (MySQL), 60 bytes
CREATE FUNCTION G(X REAL) RETURNS REAL RETURN POWER(X,1./X);
Caps lock is cruise control for cool golf.
Mumps, 14 Bytes
R I W I**(1/I)
R reads from input into variable I, and the W (write) statement does the mathematical calculations.
I will warn you: the output will be ugly as there are no implied newlines in Mumps, and at least the way I read the challenge, the OP didn't require them. If you did want cleaner output, add 2 bytes and add a newline:
R I W !,I**(1/I)
Pyth, 3 bytes
@QQ
Trivial challenge, trivial solution...
(noncompeting, 1 byte)
@
This uses the implicit input feature present in a version of Pyth that postdates this challenge.
Racket, 28 22 bytes
(λ(n)(expt n(/ 1 n)))
@cat gets credit for the 6 byte save. Thanks!
Seriously, Racket is impossible to golf with :). Nothing to see here, move along!
Convex, 3 bytes
Convex is a new language that I am developing that is heavily based on CJam and Golfscript. The interpreter and IDE can be found here. Input is an integer into the command line arguments. Indexes are one-based. Uses the CP-1252 encoding. This answer in non-competing as it was created after this challenge.
_¹#
Explanation:
Implied input
_ Duplicate top of stack
¹ Find reciprocal
# Power
Implied output
Factor, 14 10 bytes
1 over / ^
Expects input on top of the stack.
dc, 8 6 chars
Outputs to the top of the stack:
?d1r/^
Or if I can expect the input at the top of the stack, this 5 char solution works too:
d1r/^
Explanation:
? take input from STDIN and execute it (numbers are executed by pushing to the stack)
d1r duplicate top of stack, push 1 and swap them
/^ divide and exponentiate
Alas, dc does not allow for fractional exponents so this answer does not actually work for any given inputs above 1, and gives the wrong answer for 0 < input < 1. It does the same thing as many of the other programs though.
Lua, 20
n=(...)print(n^n^-1)
How it works:
n=(...) Take input and store at n
print(
n^n^-1 n^(n^(-1))
)
Brachylog, 8 7 bytes
Thanks to Fatalize for saving a byte.
^?^-1=.
Explanation
^?^-1=.
=. unify output with
^?^-1 input raised to the 1/input power
PARI/GP, 9 bytes
x->x^x^-1
This is shorter than the alternatives x->x^(1/x) and x->sqrtn(x,x). It uses the right-associativity of ^ and the strong binding of the unary -. GP isn't quite functional enough to curry (a,b)->sqrtn(a,b) into (x)->sqrtn(x,x); the best it can manage is x->call(sqrtn,[x,x]) which is far too long.
C# - 18 43 41 bytes
float a(float x){return Math.Pow(x,1/x);}
-2 byes thanks to @VoteToClose
Note:
First actual attempt at golfing - I know I could do this better.
CJam, 6 bytes
rd_W##
How it works
rd e# Read a double D from STDIN and push it on the stack.
_ e# Push a copy of D.
W e# Push -1.
# e# Compute D ** -1.
# e# Compute D ** (D ** -1).
Vitsy, 4 + 1 (function declaration) = 5 bytes
Hey, look, my language actually does this. Go figure.
Di^^
D Duplicate the top item (input)
i Push -1.
^ Pop top (-1), put the second to top to the power of the popped item (x^-1)
^ Same as above, except we now have the desired number.
This is a function that leaves the value requested on the stack. For view-ability, I have also made the program output on the Try it Online! link.
Scala, 33 bytes
def f=(x:Double)=>Math.pow(x,1/x)
C#, 25 bytes, type: double
Bare form of a lambda expression:
x=>System.Math.Pow(x,1/x)
The expression can be assigned to a function variable, e.g. (Func<double, double>), ...
System. can be omitted, if using System; is used (but more bytes).
Complete test program (143 bytes) with the input of the question:
using System;class P{static void Main(){Array.ForEach(new double[]{-.2,-.5,.5,1,2,Math.E,3,100,10000},x=>Console.WriteLine(Math.Pow(x,1/x)));}}
Result:
-3125
4
0,25
1
1,4142135623731
1,44466786100977
1,44224957030741
1,0471285480509
1,0009214583193
C#, 37 bytes, type: Complex
x=>System.Numerics.Complex.Pow(x,1/x)
Also the assembly for System.Numerics needs to be referenced.
The division operator is overloaded for complex arguments.
Test program (189 bytes) with the input of the question for complex numbers:
using System;using C=System.Numerics.Complex;class P{static void Main(){Array.ForEach(new C[]{new C(-2,0),new C(0,1),new C(0,2),new C(1,2),new C(2,2)},x=>Console.WriteLine(C.Pow(x,1/x)));}}
Result:
(4,32963728535968E-17, -0,707106781186548)
(4,81047738096535, 0)
(2,0628722350809, -0,745007062179724)
(1,81984053615444, -0,183434723804562)
(1,57500291115344, -0,100274868564155)
Test program (264 bytes) with infinity:
using System;using C=System.Numerics.Complex;class P{static void Main(){var p=double.PositiveInfinity;var n=double.NegativeInfinity;Array.ForEach(new C[]{new C(-1/p,0),new C(0,0),new C(1/p,0),new C(p,0),new C(n,0),new C(n,2) },x=>Console.WriteLine(C.Pow(x,1/x)));}}
Result:
(0, 0)
(0, 0)
(0, 0)
(1, 0)
(1, 0)
(1, 0)
Mouse-2002, 13 bytes
x:x.b/x.&ROOT
Other programs assume the number's on the top of the stack and so shall I.
x: ~ put TOS in x
x. ~ push x
b / ~ divide by 1
x. ~ push x again
&ROOT ~ y to the x root
Pyke (commit 29), 6 bytes
D1_R^^
Explanation:
D - duplicate top
1_ - load -1
R - rotate
^ - ^**^
^ - ^**^
MATLAB, 11bytes
@(x)x^(1/x)
Creates an anonymous function. This will work with all test cases including complex numbers and infinities.
This also works with Octave. You can try online here. Simply enter something like:
f=@(x)x^(1/x)
At the command line of the online interpreter, then do:
f(-1)
To test.
Awk, 8 bytes
$1^$1^-1
A shell command to check it
awk '{print $1^$1^-1}'
Longer version (+1) with
$1^(1/$1)
Gnuplot, 13 bytes
f(x)=x**(1/x)
or
f(x)=x**x**-1
Haskell, 12 11 bytes
Thanks @LambdaFairy for doing some magic:
(**)<*>(1/)
My old version:
\x->x**(1/x)
Javascript (ES2016), 11 bytes
x=>x**(1/x)
I rarely ever use ES7 over ES6.
O, 6 bytes
j.1\/^
No online link because the online IDE doesn't work (specifically, exponentiation is broken)
Explanation:
j.1\/^
j. push two copies of input
1\/ push 1/input (always float division)
^ push pow(input, 1/input)
Python 2 - 56 bytes
The first actual answer, if I'm correct. Uses Newton's method.
n=x=input();exec"x-=(x**n-n)/(1.*n*x**-~n);"*999;print x
NARS APL, 2 bytes
√⍨
NARS supports the √ function, which gives the ⍺-th root of ⍵. Applying commute (⍨) gives a function that, when used monadically, applies its argument to both sides of the given function. Therefore √⍨ x ↔ x √ x.
Other APLs, 3 bytes
⊢*÷
This is a function train, i.e. (F G H) x ↔ (F x) G H x. Monadic ⊢ is identity, dyadic * is power, and monadic ÷ is inverse. Therefore, ⊢*÷ is x raised to 1/x.
Milky Way 1.6.5, 5 bytes
'1'/h
Explanation
' ` Push input
1 ` Push the integer literal
' ` Push input
/ ` Divide the STOS by the TOS
h ` Push the STOS to the power of the TOS
x**(1/x)
Usage
$ ./mw <path-to-code> -i <input-integer>
Java, 41 bytes
float f(float n){return Math.pow(n,1/n);}
Not exactly competitive because Java, but why not?
Ruby, 15 Bytes
a=->n{n**n**-1}
Ungolfed:
-> is the stabby lambda operator where a=->n is equivalent to a = lambda {|n|}
PHP 5.6, 32 30 29 bytes
function($x){echo$x**(1/$x);}
or
function($x){echo$x**$x**-1;}
30->29, thank you Dennis!
TI-Basic, 4 bytes
Ans^Ansֿ ¹
Since TI-Basic is tokenized, this code is a mere four bytes.
R, 19 17 bytes
function(x)x^x^-1
-2 bytes thanks to @Flounderer
C++, 48 bytes
#include<math.h>
[](auto x){return pow(x,1./x);}
The second line defines an anonymous lambda function. It can be used by assigning it to a function pointer and calling it, or just calling it directly.
Java 8, 18 bytes
n->Math.pow(n,1/n)
Java isn't in last place?!?!
Test with the following:
import java.lang.Math;
public class Main {
public static void main (String[] args) {
Test test = n->Math.pow(n,1/n);
System.out.println(test.xthRoot(6.0));
}
}
interface Test {
double xthRoot(double x);
}
Japt, 3 bytes
UqU
Very simple: U is the input integer, and q is the root function on numbers.
JavaScript ES6, 18 bytes
n=>Math.pow(n,1/n)
TI-BASIC, 3 bytes
Ans×√Ans
TI-BASIC uses tokens, so Ans and ×√ are both one byte.
Explanation
Ans is the easiest way to give input; it is the result of the last expression. ×√ is a function for the x'th root of x, so for example 5×√32 is 2.
Pylons, 5 bytes.
ideAe
How it works.
i # Get command line input.
d # Duplicate the top of the stack.
e # Raise the top of the stack to the power of the second to the top element of the stack.
A # Push -1 to the stack (pre initialized variable).
e # Raise the top of the stack to the power of the second to the top element of the stack.
# Implicitly print the stack.
Codename Dragon, 16 bytes
a=<>;dispa^(1/a)
I'm abusing the lack of a syntax error.
Ungolfed:
disp((a=<>)^(1/a))
This is a simple program that takes input through <>.
You can find an interpreter here, no github site yet.
Perl 5, 10 bytes
9 bytes plus 1 for -p
$_**=1/$_
Jelly, 2 bytes
*İ
How it works
*İ Main link. Input: n
İ Inverse; yield 1÷n.
* Power (fork); compute n ** (1÷n).
Python 3, 17 bytes
lambda x:x**(1/x)
Self-explanatory