# The challenge

Quite simple, given an input x, calculate it's infinite power tower!

x^x^x^x^x^x...


For you math-lovers out there, this is x's infinite tetration.

Keep in mind the following:

x^x^x^x^x^x... = x^(x^(x^(x^(x...)))) != (((((x)^x)^x)^x)^x...)


Surprised we haven't had a "simple" math challenge involving this!*

# Assumptions

• x will always converge.
• Negative and complex numbers should be able to be handled
• This is , so lowest bytes wins!
• Your answers should be correct to at least 5 decimal places

# Examples

Input >> Output

1.4 >> 1.8866633062463325
1.414 >> 1.9980364085457847
[Square root of 2] >> 2
-1 >> -1
i >> 0.4382829367270323 + 0.3605924718713857i
1 >> 1
0.5 >> 0.641185744504986
0.333... >> 0.5478086216540975
1 + i >> 0.6410264788204891 + 0.5236284612571633i
-i >> 0.4382829367270323 -0.3605924718713857i
[4th root of 2] >> 1.239627729522762


*(Other than a more complicated challenge here)

• I don’t think this tower converges at x = −2 or x = −0.5. – Anders Kaseorg Jul 25 '17 at 7:37
• @AndersKaseorg I agree, though all programs seem to have the same converging answer. Why don't they converge? – Graviton Jul 25 '17 at 7:38
• x = −2 gets attracted to a 8-cycle and x = −0.5 gets attracted to a 6-cycle. (My program still gives an answer in these cases, but it’s one of the points in the cycle and not a fixed point; this doesn’t indicate convergence.) – Anders Kaseorg Jul 25 '17 at 7:44
• @AndersKaseorg Aha very interesting. You wouldn't happen to know why '8' for -2 and '6' for -0.5? Just out of curiosity of course. – Graviton Jul 25 '17 at 7:47
• You can run the iterations just as easily as I can, but here’s a picture: commons.wikimedia.org/wiki/File:Tetration_period.png – Anders Kaseorg Jul 25 '17 at 7:50

# APL (Dyalog), 4 bytes

*⍣≡⍨


Try it online!

* power

⍣ until

≡ stable

⍨ selfie

# Pyth,  4  3 bytes

crossed out 4 is still regular 4 ;(

u^Q


Try it online

### How it works

u       first repeated value under repeated application of G ↦
^QG        input ** G
Q   starting at input

• You don't need the last G, it will get auto-filled. – FryAmTheEggman Jul 25 '17 at 13:10
• @FryAmTheEggman Right, thanks! – Anders Kaseorg Jul 25 '17 at 20:14

For inputs that don't converge (eg. -2) this won't terminate:

import Data.Complex
f x=until(\a->magnitude(a-x**a)<1e-6)(x**)x


Thanks a lot @ØrjanJohansen for teaching me about until and saving me 37 bytes!

Try it online!

• You can shorten this a lot with the until function. Try it online! – Ørjan Johansen Jul 25 '17 at 8:10
• Neat! Did not know until, thanks a lot. – ბიმო Jul 25 '17 at 8:16

# Python 3, 40 39 35 bytes

• Thanks @Ørjan Johansen for a byte: d>99 instead of d==99: 1 more iteration for a lesser byte-count
• Thanks @Uriel for 4 bytes: wise utilization of the fact that x**True evaluates to x in x**(d>99or g(x,d+1)). The expression in the term evaluates to True for depth greater than 99 and thus returns the passed value.

Recursive lambda with a max-depth 100 i.e. for a depth 100 returns the same value. Actually is convergency-agnostic, so expect the unexpected for numbers with non-converging values for the function.

g=lambda x,d=0:x**(d>99or g(x,d+1))


Try it online!

• In the tio link, you can replace complex('j') with 1j – Mr. Xcoder Jul 25 '17 at 8:26
• d>99 does one more iteration and is shorter. – Ørjan Johansen Jul 25 '17 at 8:43
• save 4 bytes with g=lambda x,d=0:x**(d>99or g(x,d+1)), x**True evaluates to x – Uriel Jul 25 '17 at 12:57
• @Uriel, That is really smart..... Thanks!!! – officialaimm Jul 25 '17 at 13:09

# Python 3, 3730 27 bytes

-7 bytes from @FelipeNardiBatista.
-3 bytes from from @xnor

I don't remember much of Python anymore, but I managed to port my Ruby answer and beat the other Python 3 answer :D

lambda x:eval('x**'*99+'1')


Try it online!

• FYI, it appears that f-strings were first introduced in Python 3.6: see python.org/dev/peps/pep-0498 . (This would explain why your code didn't work for me in 3.5.2.) Just thought I'd mention this in case anyone else was confused. – mathmandan Jul 25 '17 at 16:01
• You don't need to substitute in the value of x, eval('x**'*99+'1') works – xnor Jul 25 '17 at 18:51
• @xnor doh, of course it does :) thanks – daniero Jul 25 '17 at 19:08
• @xnor Neat -- I applied the same thing in my Ruby answer and it somehow fixed it :) – daniero Jul 25 '17 at 19:17
• +1, I am slapping myself for forgetting the existence of eval.... :D – officialaimm Jul 26 '17 at 5:29

# Mathematica, 12 bytes

#//.x_:>#^x&


Takes a floating‐point number as input.

# J, 5 bytes

^^:_~


Try it online!

## Explanation

First, I'll show what command is being executed after parsing the ~ at the end, and the walk-through will be for the new verb.

(^^:_~) x = ((x&^)^:_) x

((x&^)^:_) x  |  Input: x
^:_     |  Execute starting with y = x until the result converges
x&^         |    Compute y = x^y

• The J solution is really nice here. To break down your first line in finer grain, is it correct to say that the following happens: (^^:_)  creates a new dyadic verb via the power conj, then self adverb ~ makes that verb monadic, so that when given an argument x it's expanded to x (^^:_) x. the left x subsequently "sticks", giving ((x&^)^:_) x per your note, and only the right argument changes during iteration? – Jonah Jul 25 '17 at 14:30
• @Jonah Sure, when giving two arguments to a dyad with power, x u^:n y, the left argument is bonded with the dyad to form a monad that is nested n times on y. x u^:n y -> (x&u)^:n y -> (x&u) ... n times ... (x&u) y – miles Jul 25 '17 at 14:36

# C# (.NET Core), 79 78 bytes

x=>{var a=x;for(int i=0;i++<999;)a=System.Numerics.Complex.Pow(x,a);return a;}


Try it online!

I chose to iterate until i=999 because if I iterated until 99 some examples did not reach the required precision. Example:

Input:                      (0, 1)
Expected output:            (0.4382829367270323, 0.3605924718713857)
Output after 99 iterations: (0.438288569331222,  0.360588154553794)
Output after 999 iter.:     (0.438282936727032,  0.360592471871385)


As you can see, after 99 iterations the imaginary part failed in the 5th decimal place.

Input:                      (1, 1)
Expected output:            (0.6410264788204891, 0.5236284612571633)
Output after 99 iterations: (0.64102647882049,   0.523628461257164)
Output after 999 iter.:     (0.641026478820489,  0.523628461257163)


In this case after 99 iterations we get the expected precision. In fact, I could iterate until i=1e9 with the same byte count, but that would make the code considerably slower

• 1 byte saved thanks to an anonymous user.
• +1 For the complex class I didn't even know that existed. – TheLethalCoder Jul 25 '17 at 11:29
• @TheLethalCoder neither did I until I googled it. :-) – Charlie Jul 25 '17 at 11:30

³*$ÐL  Try it online! # Ruby, 21 20 bytes ->n{eval'n**'*99+?1}  Disclaimer: It seems that Ruby returns some weird values when raising a complex number to a power. I assume it's out of scope for this challenge to fix Ruby's entire math module, but otherwise the results of this function should be correct. Edit: Applied the latest changes from my Python 3 answer and suddenly it somehow gives the same, expected results :) Try it online! • Take out the space after the eval. – Value Ink Jul 25 '17 at 21:41 • Your original version failed on the complex test case because it evaled the string "0+1i**0+1i**0+1i**...", which parses in the wrong way since ** has higher precedence than +. – Ørjan Johansen Jul 26 '17 at 0:39 • @ØrjanJohansen huh, you're right. I guess I was fooled by the fact that #inspect and #to_s return different values. Before submitting the initial answer I did some testing in irb and saw that e.g entering Complex(1,2) in the REPL would give (1+2i), including the parentheses. When stringifying the value however the parentheses are not included, so the precedence, as you point out, messed it up. – daniero Jul 26 '17 at 2:37 • I thought eval use was forbidden. – V. Courtois Jul 26 '17 at 9:07 • @V.Courtois Ok. But it's not. – daniero Jul 26 '17 at 10:10 # TI-BASIC, 16 bytes The input and output are stored in Ans. Ans→X While Ans≠X^Ans X^Ans End  # R, 36 33 bytes -3 bytes thanks to Jarko Dubbeldam Reduce(^,rep(scan(,1i),999),,T)  Reads from stdin. Reduces from the right to get the exponents applied in the correct order. Try it (function) Try it (stdin) • scan(,1i) works. Similar to how scan(,'') works. – JAD Jul 25 '17 at 19:20 • @JarkoDubbeldam of course! sometimes my brain doesn't work. – Giuseppe Jul 25 '17 at 19:24 # Javascript, 33 bytes f=(x,y=x)=>(x**y===y)?y:f(x,x**y)  • JavaScript doesn't handle imaginary numbers. – kamoroso94 Jul 25 '17 at 22:24 # MATL, 20 10 bytes cut down to half thanks to @LuisMendo t^Gw^t5M-  Try it online! This is my first and my first time using MATL so i'm sure it could be easily outgolfed. • Welcome to the site, and nice first answer! A few suggestions: XII is equivalent to t. You can also get rid of XH and H using the automatic clipboard M, that is, ttt^yw^t5M-]bb-x. And in the last part, instead of deleting the unwanted values you can use &, which tells the implicit display function to only show the top. So, you can use ttt^yw^t5M-]& and save a few bytes. – Luis Mendo Jul 27 '17 at 15:01 • Also, the first t is not needed, and using G instead of another t you can avoid & and thus leave ] implicit: t^Gw^t5M-. Hey, we've reduced byte count by a half! – Luis Mendo Jul 27 '17 at 15:09 • @LuisMendo Thanks for the great tips! I have a lot to learn about MATL, but I really like it. – Cinaski Jul 28 '17 at 9:11 • Glad to hear that! – Luis Mendo Jul 28 '17 at 9:11 # Perl 6, 17 bytes {[R**]$_ xx 999}


Try it online!

R** is the reverse-exponentiation operator; x R** y is equal to y ** x. [R**] reduces a list of 999 copies of the input argument with reverse exponentiation.