Golf the xᵗʰ root of x

24

4

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.

Adám

Posted 2016-02-23T03:54:15.370

Reputation: 37 779

1

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).

– Jeppe Stig Nielsen – 2016-02-24T14:36:47.430

What about for languages that do not handle power of decimals? – Leaky Nun – 2016-03-31T03:24:11.433

1@KennyLau Feel free to post with a note that says so, especially if the algorithm would work, had the language supported it. – Adám – 2016-03-31T05:37:09.347

Answers

38

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.

NinjaBearMonkey

Posted 2016-02-23T03:54:15.370

Reputation: 9 925

8As far as I am aware ans would count as hardcoding inputs into variables and does not seem to be an accepted input method for [tag:code-golf]. In that case, please make a full program or a function. – flawr – 2016-02-24T09:14:49.207

4@flawr I can see what you're saying but it seems it's always been done like this. Maybe it warrants a meta post? – NinjaBearMonkey – 2016-02-24T16:08:45.570

3Ans is STDIN/STDOUT for TI-Basic. – Timtech – 2016-02-24T22:43:27.500

5stdin and stdout are text streams, usually for interactive text input and output. Ans is not interactive, unlike some other functions in TI-BASIC, which are interactive. – Olathe – 2016-02-25T05:21:50.280

@Timtech Certainly not. There are functions like Input and Prompt. – flawr – 2016-02-25T20:59:12.893

You can vote on the validity of Ans on meta.

– NinjaBearMonkey – 2016-02-25T22:54:06.680

What I meant is that Ans is the only way for functions to take input and return output without messing with the global variables (technically Ans is global, but I'm referring to the A, B, etc.) – Timtech – 2016-02-26T16:06:42.970

Question: Are there more than 256 tokens? If so, your byte count is invalid. – CalculatorFeline – 2016-02-29T21:30:20.743

@CatsAreFluffy Yes, but they are two bytes. There are 256 (actually a little less) one-byte tokens. – NinjaBearMonkey – 2016-02-29T21:34:46.987

7@flawr The reason Ans is usually accepted is because its value is set by any expression (expressions are separated by :). Therefore something like 1337:prgmXTHROOT would input 1337, which looks a lot like input via CLAs in a normal language. – lirtosiast – 2016-03-05T05:06:42.870

@CatsAreFluffy http://tibasicdev.wikidot.com/tokens

– Timtech – 2016-03-16T18:10:33.543

23

Jelly, 2 bytes

Try it online!

How it works

*İ    Main link. Input: n

 İ    Inverse; yield 1÷n.
*     Power (fork); compute n ** (1÷n).

Dennis

Posted 2016-02-23T03:54:15.370

Reputation: 196 637

Jelly doesn't have a stack. A dyad follow by a monad in a monadic chain behaves like APL's forks. – Dennis – 2016-02-23T04:46:54.130

3No, J's ^% is a hook (which do not exist in Dyalog APL), not a fork. Jelly and APL code is difficult to compare since Jelly is left-to-right. The nearest equivalent would be ÷*⊢ (also a fork), which computes (1/x)**x because of the different direction. Since Jelly's atoms aren't overloaded (they are either monadic or dyadic, but never both), there can be monadic 1,2,1- and 2,1-forks. – Dennis – 2016-02-23T04:58:46.847

Thanks for the clarification. Naturally, I'm quite intrigued by Jelly (which I still think should be named ȷ or something similar.) – Adám – 2016-02-23T05:07:54.790

17

Javascript (ES2016), 11 bytes

x=>x**(1/x)

I rarely ever use ES7 over ES6.

Mama Fun Roll

Posted 2016-02-23T03:54:15.370

Reputation: 7 234

2x=>x**x**-1 also works, again for 11 bytes. – Neil – 2016-02-23T08:51:16.740

7All hail the new exponentiation operator! – mbomb007 – 2016-02-23T21:33:17.170

15

Python 3, 17 bytes

lambda x:x**(1/x)

Self-explanatory

Mego

Posted 2016-02-23T03:54:15.370

Reputation: 32 998

7I quite like lambda x:x**x**-1, but it's not shorter. – seequ – 2016-02-23T06:32:05.513

1@Seeq Your expression is the same length, but it has the advantage of working in both Python 2 and 3. – mathmandan – 2016-02-23T17:54:02.537

1Python 2's shortest is lambda x:x**x**-1, so it is the same in 2 and 3. – mbomb007 – 2016-02-23T21:31:37.830

I couldn't find this answer for ages and was really annoyed when I did. – None – 2017-02-05T21:09:36.683

12

Haskell, 12 11 bytes

Thanks @LambdaFairy for doing some magic:

(**)<*>(1/) 

My old version:

\x->x**(1/x)

flawr

Posted 2016-02-23T03:54:15.370

Reputation: 40 560

4(**)<*>(1/) is 11 bytes. – Lambda Fairy – 2016-02-24T01:32:07.793

@LambdaFairy Thanks! Do you mind explaining? It looks like you are doing some magic with partially applied functions but as I am quite new to Haskell I do not really understand how this works=) – flawr – 2016-02-24T09:02:48.743

This uses the fact that a 1-argument function can be considered an applicative functor (the "reader monad"). The <*> operator takes an applicative that produces a function, and an applicative that produces a value, and applies the function to the value. So in this case, a mind-bending way to apply a 2-argument function to a 1-argument function. – MathematicalOrchid – 2016-02-24T16:43:30.883

2The function <*> takes 3 arguments, two functions f and g and an argument x. It is defined as (<*>) f g x = f x (g x), i.e. it applies f to x and g x. Here it's partially applied to f and g leaving out x, where f = (**) and g = (1/) (another partially applied function (a section) that calculates the reciprocal value of it's argument). So ( (**)<*>(1/) ) x is (**) x ((1/) x) or written in infix: x ** ((1/) x) and with the section resolved: x ** (1/x). -- Note: <*> is used in function context here and behaves differently in other contexts. – nimi – 2016-02-24T16:52:17.510

@nimi So it's the equivalent of the S combinator i.e. S(**)(1/)? – Neil – 2016-03-02T13:03:40.397

@Neil: yes.

– nimi – 2016-03-02T16:54:15.247

10

J, 2 bytes

^%

Try it online!.

How it works

^%  Monadic verb. Argument: y

 %  Inverse; yield 1÷y.
^   Power (hook); compute y ** (1÷y).

Dennis

Posted 2016-02-23T03:54:15.370

Reputation: 196 637

I was gonna write this answer. I'm too slow at this. – Bijan – 2017-03-19T22:02:27.933

1@Bijan Over a year too slow, it seems. :P – Dennis – 2017-03-19T22:21:09.167

I see, I've only been golfing for a week now. – Bijan – 2017-03-19T22:37:10.730

9

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.

Doorknob

Posted 2016-02-23T03:54:15.370

Reputation: 68 138

Does this solution predate the feature of implicit input? – Leaky Nun – 2016-04-07T23:39:22.557

@KennyLau Yes, by a long time. But I've edited the one-byte solution in anyway. – Doorknob – 2016-04-08T04:40:56.070

8

JavaScript ES6, 18 bytes

n=>Math.pow(n,1/n)

Downgoat

Posted 2016-02-23T03:54:15.370

Reputation: 27 116

8

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);
}

GamrCorps

Posted 2016-02-23T03:54:15.370

Reputation: 7 058

It's the fact that it's a function – CalculatorFeline – 2016-02-29T21:30:41.307

6

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. :(

CalculatorFeline

Posted 2016-02-23T03:54:15.370

Reputation: 2 608

1#^#^-1& saves 1 byte. – njpipeorgan – 2016-02-23T15:16:56.813

NOW it is golfed. – Adám – 2016-02-23T23:16:08.040

1NOW it is golfed. – CalculatorFeline – 2016-03-10T02:43:14.013

1When Mthmtca is released, we are going to rule this board. – Michael Stern – 2016-03-16T03:48:38.253

Hmm...time to look through my answers and make a compressed version of every builtin... – CalculatorFeline – 2016-03-16T04:58:50.877

1Surely just Surd isn't valid as it requires two arguments? – LLlAMnYP – 2016-06-07T14:16:24.650

Oh...reverting. – CalculatorFeline – 2017-01-13T18:59:37.990

6

MATL, 5 bytes

t-1^^

Try it online!

t       % implicit input x, duplicate
 -1     % push -1
   ^    % power (raise x to -1): gives 1/x
    ^   % power (raise x to 1/x). Implicit display

Luis Mendo

Posted 2016-02-23T03:54:15.370

Reputation: 87 464

6

Java, 41 bytes

float f(float n){return Math.pow(n,1/n);}

Not exactly competitive because Java, but why not?

Darrel Hoffman

Posted 2016-02-23T03:54:15.370

Reputation: 353

1Welcome to PPCG! I think you might be missing a return type on this function. – a spaghetto – 2016-02-23T18:28:16.183

Oops, got sloppy. A Java 8 answer already beat this one of course... – Darrel Hoffman – 2016-02-23T23:18:43.530

5

R, 19 17 bytes

function(x)x^x^-1

-2 bytes thanks to @Flounderer

mnel

Posted 2016-02-23T03:54:15.370

Reputation: 826

Why not x^(1/x) ? Edit: x^x^-1 seems to work too. – Flounderer – 2016-02-23T20:10:46.433

That's a snippet, and apparently people don't like snippets. – CalculatorFeline – 2016-02-29T21:28:17.647

@CatsAreFluffy it is the definition of a function. – mnel – 2016-02-29T21:30:30.750

5

CJam, 6 bytes

rd_W##

Try it online!

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).

Dennis

Posted 2016-02-23T03:54:15.370

Reputation: 196 637

5

Perl 5, 10 bytes

9 bytes plus 1 for -p

$_**=1/$_

msh210

Posted 2016-02-23T03:54:15.370

Reputation: 3 094

5

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 √⍨ xx √ 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.

marinus

Posted 2016-02-23T03:54:15.370

Reputation: 30 224

5

Ruby, 15 Bytes

a=->n{n**n**-1}

Ungolfed:

-> is the stabby lambda operator where a=->n is equivalent to a = lambda {|n|}

Eli Sadoff

Posted 2016-02-23T03:54:15.370

Reputation: 151

5

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

Maltysen

Posted 2016-02-23T03:54:15.370

Reputation: 25 023

Functions are okay. – CalculatorFeline – 2016-02-24T01:18:09.090

4

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^(1/x), this takes advantage of the fact that:

Advantage

So, to calculate ln(x), this also takes advantage of the fact that:

Advantage2

whose definite integral from 1 to (b = x) is numerically-approximated in increments of 10^-5 using the following summation formula:

Summation Formula.

The resulting sum is then multiplied by 1/x to get ln(x)/x. e^(ln(x)/x) is then finally calculated using the e^x Maclaurin Series to 100 terms as follows:

e^x Maclaurin Series.

This results in our relatively accurate output of x^(1/x).

R. Kap

Posted 2016-02-23T03:54:15.370

Reputation: 4 730

1+1 This has got to be one of the best dc answers out there. I'm bookmarking this! – user41805 – 2017-03-19T20:43:47.670

@KritixiLithos Thank you! I appreciate the kind words. :) – R. Kap – 2017-03-19T20:47:40.650

4

Seriously, 5 bytes

,;ì@^

Try it online!

Explanation:

,;ì@^
,;     input, dupe
  ì@   1/x, swap
    ^  pow

Mego

Posted 2016-02-23T03:54:15.370

Reputation: 32 998

4

, 5 chars / 7 bytes

Мű⁽ïï

Try it here (Firefox only).

Trivial.

Mama Fun Roll

Posted 2016-02-23T03:54:15.370

Reputation: 7 234

4

Pyke (commit 29), 6 bytes

D1_R^^

Explanation:

D      - duplicate top
 1_    - load -1
   R   - rotate
    ^  - ^**^
     ^ - ^**^

Blue

Posted 2016-02-23T03:54:15.370

Reputation: 26 661

Can haz link pls? – cat – 2016-02-24T17:33:10.313

Oh, I thought you meant there's no implementation available. Yes, the interpreter doesn't have to be hosted, just a link to the repo / source (or docs) will suffice – cat – 2016-02-24T17:36:59.130

4

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.

Morgan Thrapp

Posted 2016-02-23T03:54:15.370

Reputation: 3 574

4

Japt, 3 bytes

UqU

Test it online!

Very simple: U is the input integer, and q is the root function on numbers.

ETHproductions

Posted 2016-02-23T03:54:15.370

Reputation: 47 880

4

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.

Try it online

Mego

Posted 2016-02-23T03:54:15.370

Reputation: 32 998

Does ^ not work in C++ as it does in C? – takra – 2016-02-23T23:18:12.400

2@minerguy31: ^ is bitwise xor in C (and C++). – marinus – 2016-02-23T23:19:51.943

4

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.

Try it online

Mego

Posted 2016-02-23T03:54:15.370

Reputation: 32 998

With GCC, you don't need to include math.h. – Dennis – 2016-03-10T03:04:52.387

-1 byte: #define p(a)pow(a,1./a) – Erik the Outgolfer – 2016-06-14T08:37:50.570

4

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>

Zach Gates

Posted 2016-02-23T03:54:15.370

Reputation: 6 152

4

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)

Mego

Posted 2016-02-23T03:54:15.370

Reputation: 32 998

oh hey you did it yay – phase – 2016-03-19T04:20:08.313

4

C# - 18 43 41 bytes

float a(float x){return Math.Pow(x,1/x);}

-2 byes thanks to @VoteToClose

Try it out

Note:

First actual attempt at golfing - I know I could do this better.

EnragedTanker

Posted 2016-02-23T03:54:15.370

Reputation: 309

Welcome to the crowd! It is exactly because of newcomers that I make trivial challenges like this. – Adám – 2016-02-24T19:29:39.717

Fixed. Thanks for informing me about this – EnragedTanker – 2016-02-25T15:40:11.607

@crayzeedude No problem at all. Nice job and again, welcome to PPCG! – Alex A. – 2016-02-25T18:00:09.770

Does C# have float? – Addison Crump – 2016-02-27T00:31:20.700

Indeed it does. – EnragedTanker – 2016-02-27T18:29:42.747

3

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.

Charles

Posted 2016-02-23T03:54:15.370

Reputation: 2 435

3

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!

ricdesi

Posted 2016-02-23T03:54:15.370

Reputation: 499

3

Awk, 8 bytes

$1^$1^-1

A shell command to check it

awk '{print $1^$1^-1}'

Longer version (+1) with

$1^(1/$1)

Hastur

Posted 2016-02-23T03:54:15.370

Reputation: 277

3

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.

Tom Carpenter

Posted 2016-02-23T03:54:15.370

Reputation: 3 990

3

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)

Heiko Oberdiek

Posted 2016-02-23T03:54:15.370

Reputation: 3 841

3

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.

Try it Online!

Addison Crump

Posted 2016-02-23T03:54:15.370

Reputation: 10 763

-1: Anonymous functions are allowed. – Erik the Outgolfer – 2016-06-14T08:51:10.403

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Unfortunately, that's what I thought. However, I was informed that I would have to include the return character before the function in order for it to be valid. – Addison Crump – 2016-06-15T16:21:06.850

2

Bean, 11 bytes (non-competing)

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.

Patrick Roberts

Posted 2016-02-23T03:54:15.370

Reputation: 2 475

Sorry, just noticed the post date of the question. I had previously assumed it was new – Patrick Roberts – 2017-02-05T12:06:53.830

2

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.

Conor O'Brien

Posted 2016-02-23T03:54:15.370

Reputation: 36 228

2

Jolf, 5 bytes

Try it here!

^j/1j
 j    input
^ /1j to the 1/j pow.

Conor O'Brien

Posted 2016-02-23T03:54:15.370

Reputation: 36 228

2

Gnuplot, 13 bytes

f(x)=x**(1/x)

or

f(x)=x**x**-1

Hastur

Posted 2016-02-23T03:54:15.370

Reputation: 277

2

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!

Winny

Posted 2016-02-23T03:54:15.370

Reputation: 1 120

1You can do (λ(n)(expt n(/ 1 n))) as an anonymous function for 22 bytes – cat – 2016-04-05T21:43:32.423

1

We need a golfing dialect of Racket, like we need a golfing dialect of Factor except people have already made LISPs for golf and the code becomes mostly parentheses.

– cat – 2016-04-06T02:08:31.583

1I forgot that anonymous functions are generally okay. We definitely could use a lovely LISP for golfing. But yeah, I can only imagine the parens are still going to reduce competitiveness. – Winny – 2016-04-06T06:44:44.997

2

Factor, 14 10 bytes

1 over / ^

Expects input on top of the stack.

cat

Posted 2016-02-23T03:54:15.370

Reputation: 4 989

2

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

GamrCorps

Posted 2016-02-23T03:54:15.370

Reputation: 7 058

Can be found... where? – cat – 2016-04-05T21:47:21.627

@cat oops. Fixed. Note that I literally just updated the interpreter though, but this still works. – GamrCorps – 2016-04-06T02:04:36.673

2

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

a spaghetto

Posted 2016-02-23T03:54:15.370

Reputation: 10 647

You can save one byte: you don't need the first ?, it is implicitely the current variable at the beginning of the predicate. – Fatalize – 2016-03-16T20:28:34.763

2

Lua, 20

n=(...)print(n^n^-1)

How it works:

n=(...)              Take input and store at n
       print(
             n^n^-1  n^(n^(-1))
                   )

Leaky Nun

Posted 2016-02-23T03:54:15.370

Reputation: 45 011

2

(Noncompeting) Quetzalcoatl, 4 bytes

c.I?

Explanation:

c    # Input
 .   # Duplicate
  I  # Inverse
   ? # Power

This answer was too short to post before adding this sentence.

NoOneIsHere

Posted 2016-02-23T03:54:15.370

Reputation: 1 916

So... "Quetzalcoatl" can have an operator which finds x^x^-1... – Erik the Outgolfer – 2016-06-14T08:57:50.123

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ What operator are you referring to? – NoOneIsHere – 2016-06-14T14:39:37.513

Like Python's a**a**-1. – Erik the Outgolfer – 2016-06-14T14:43:36.960

2

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.

mriklojn

Posted 2016-02-23T03:54:15.370

Reputation: 381

2

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.

george

Posted 2016-02-23T03:54:15.370

Reputation: 1 495

Why the int(...)? – Adám – 2016-06-14T07:19:36.183

Python 3 won't implicitly take an input as an integer. So it needs to be converted for the arithmetic in the second statement. Unless it does work for this scenarios in which case my solution will be shorter – george – 2016-06-14T07:29:55.753

OP: for any number in your language's domain – Adám – 2016-06-14T07:37:55.177

@Adám I'm not sure that I understand that part then. I just thought it meant it would take any number in the normal base. For Python that is base 10. – george – 2016-06-14T08:05:29.837

If Python natively handles real numbers, then input may be a real (i.e. non-integer). – Adám – 2016-06-14T08:11:35.160

@Adám oh of course. Well in that case as all integers are floats, but not all floats are integers. I can expect a float input – george – 2016-06-14T08:12:39.967

Doesn't Python handle complex numbers as well?

– Adám – 2016-06-14T08:39:09.480

Yeah, complex, +4 BYTES NOW!!!!!!! – Erik the Outgolfer – 2016-06-14T09:02:20.200

@Adám The output will be different. – Erik the Outgolfer – 2016-06-14T09:02:40.310

wow I didn't know Python could handle complex numbers. @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ what do I need to change. If I change float to complex the code no longer works? – george – 2016-06-14T09:40:26.727

@george If you want to handle complex (like 3+1j or 7j) numbers, simply replace float with complex. The result of complex(17) will be (17+0j), while complex(11j) results in 11j. Note that 11 is an int, while 11j and (11+2j) are both complex. You can easily replace (1+2j) with 1+2j. – Erik the Outgolfer – 2016-06-14T10:02:13.770

Ah I did try that, but in my maths classes I use "i" as the imaginary number and I tried this and it chucked an error – george – 2016-06-14T10:03:51.007

@george It's not 14i, it's 14j. – Erik the Outgolfer – 2016-06-14T10:09:00.373

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ sorry my last post was clear. I meant when I first tried using complex I got an error because I had used 14i but when you posted afterwards explains it with "j" I figured that was what it was. Thank you – george – 2016-06-14T10:10:44.880

2

05AB1E, 3 bytes (non-competing)

Non-competing, because 05AB1E couldn't parse floats correctly at the time the challenge was posted. Code:

Dzm

Explanation:

D    # Duplicate the input
 z   # Inverse, 1 ÷ input
  m  # Power, calculating input ** (1 ÷ input)

Try it online!.

Adnan

Posted 2016-02-23T03:54:15.370

Reputation: 41 965

Wait a minute! If it couldn't parse floats, then floats were not in the domain, and so an integer solution was acceptable. – Adám – 2016-06-14T11:01:28.797

@Adám Oh, I didn't know that! The code would still be the same though :p. – Adnan – 2016-06-14T11:02:52.830

Weird how -.2 still returns -3124.999999999999 – Magic Octopus Urn – 2017-03-21T14:36:06.063

And, also, I think he meant you can remove the non-competing tag. – Magic Octopus Urn – 2017-03-21T14:36:39.547

@carusocomputing Hmm, how is that weird then? – Adnan – 2017-03-21T14:42:33.727

Oh, nevermind, python does the same; I thought Python was evaluating at -3125, but it isn't. – Magic Octopus Urn – 2017-03-21T14:48:11.297

zm works now. – Magic Octopus Urn – 2017-09-20T13:34:25.763

1

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

sergiol

Posted 2016-02-23T03:54:15.370

Reputation: 3 055

1

SmileBASIC, 18 bytes

INPUT X?POW(X,1/X)

12Me21

Posted 2016-02-23T03:54:15.370

Reputation: 6 110

1

Common Lisp, 24 bytes

(lambda(x)(expt x(/ x)))

Try it online!

ceilingcat

Posted 2016-02-23T03:54:15.370

Reputation: 5 503

1

TI-Basic, 4 bytes

Ans^Ansֿ ¹

Since TI-Basic is tokenized, this code is a mere four bytes.

Timtech

Posted 2016-02-23T03:54:15.370

Reputation: 12 038

3

We already have a TI-BASIC answer at 3 bytes.

– Zach Gates – 2016-02-23T23:37:07.687

Is ans accepted as input method? – flawr – 2016-02-24T00:06:46.020

1

QBIC, 9 bytes

:?a^(1/a)

Gets a numeric input from the command line and does the math...

steenbergh

Posted 2016-02-23T03:54:15.370

Reputation: 7 772

1

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

cat

Posted 2016-02-23T03:54:15.370

Reputation: 4 989

1

Scala, 33 bytes

def f=(x:Double)=>Math.pow(x,1/x)

Michal

Posted 2016-02-23T03:54:15.370

Reputation: 209

1

Perl, 11 bytes

Added 2 bytes for -pn.

$_**=1/$_

Try it here !

Paul Picard

Posted 2016-02-23T03:54:15.370

Reputation: 863

1

(Non-competing) Pyth, 1 byte

@

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

Try it online!

Leaky Nun

Posted 2016-02-23T03:54:15.370

Reputation: 45 011

Why is it non-competing? Did you get an answer about implicit input predating the OP? – Adám – 2016-04-08T01:35:17.677

I am not sure yet, but I think Doorknob would have this solution if he has known about the implicit input. – Leaky Nun – 2016-04-08T01:45:18.427

2

@Nᴮᶻ Implicit input was introduced with this commit, which postdates this challenge by a month.

– Dennis – 2016-04-08T04:44:03.370

@KennyLau this doesn't have to non-competing. – Rɪᴋᴇʀ – 2016-04-08T17:46:12.287

1Why not? This feature postdates the challenge. – Leaky Nun – 2016-04-08T23:03:09.803

@Nᴮᶻ This is noncompeting because the language feature used is newer than the challenge, and thus is not eligible to win the challenge and get the checkmark. – Mego – 2016-04-13T04:39:46.487

1

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)

zmerch

Posted 2016-02-23T03:54:15.370

Reputation: 541

1

SQL (MySQL), 60 bytes

CREATE FUNCTION G(X REAL) RETURNS REAL RETURN POWER(X,1./X);

Caps lock is cruise control for cool golf.

Mego

Posted 2016-02-23T03:54:15.370

Reputation: 32 998

Can you do )RETURNS instead of ) RETURNS for -1 byte? – Erik the Outgolfer – 2016-06-14T08:59:43.027

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Possibly; I don't have a MySQL server handy right now to test it, though. – Mego – 2016-06-14T09:00:28.803

1

Desmos, 14 bytes

a=1
\sqrt[a]{a}

Demonstration

weatherman115

Posted 2016-02-23T03:54:15.370

Reputation: 605

That looks interesting. So \sqrt is a root-of function, and not square-root? – Adám – 2016-06-06T20:04:50.803

@Adám It is TeX. \sqrt is the Square Root symbol, and the brackets change the root. – NoOneIsHere – 2016-06-06T20:50:47.883

0

Excel, 10 bytes

=A1^(1/A1)

Self explanatory.

Wernisch

Posted 2016-02-23T03:54:15.370

Reputation: 2 534