The infinite power tower

22

3

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)

Graviton

Posted 2017-07-25T07:09:46.497

Reputation: 2 295

1I don’t think this tower converges at x = −2 or x = −0.5. – Anders Kaseorg – 2017-07-25T07:37:05.427

@AndersKaseorg I agree, though all programs seem to have the same converging answer. Why don't they converge? – Graviton – 2017-07-25T07:38:16.797

2x = −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 – 2017-07-25T07:44:03.600

@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 – 2017-07-25T07:47:20.267

2

You can run the iterations just as easily as I can, but here’s a picture: https://commons.wikimedia.org/wiki/File:Tetration_period.png

– Anders Kaseorg – 2017-07-25T07:50:51.727

You say we have to calculate the infinite power tower, but do we have to display/output it? – kamoroso94 – 2017-07-25T14:01:22.030

@kamoroso94 Yes and no. A lot of answers just have a function that generates a number, but can be outputted quite easily (usually shown in the 'Try It Online'). It really depends if it's faster to print it or to return it as a function. – Graviton – 2017-07-25T22:14:12.307

Answers

14

APL (Dyalog), 4 bytes

*⍣≡⍨

Try it online!

* power

 until

 stable

 selfie

Adám

Posted 2017-07-25T07:09:46.497

Reputation: 37 779

10

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

Anders Kaseorg

Posted 2017-07-25T07:09:46.497

Reputation: 29 242

2You don't need the last G, it will get auto-filled. – FryAmTheEggman – 2017-07-25T13:10:05.973

@FryAmTheEggman Right, thanks! – Anders Kaseorg – 2017-07-25T20:14:50.723

7

Haskell, 100 63 bytes

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!

ბიმო

Posted 2017-07-25T07:09:46.497

Reputation: 15 345

1

You can shorten this a lot with the until function. Try it online!

– Ørjan Johansen – 2017-07-25T08:10:52.380

Neat! Did not know until, thanks a lot. – ბიმო – 2017-07-25T08:16:23.793

7

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!

officialaimm

Posted 2017-07-25T07:09:46.497

Reputation: 2 739

1In the tio link, you can replace complex('j') with 1j – Mr. Xcoder – 2017-07-25T08:26:15.573

1d>99 does one more iteration and is shorter. – Ørjan Johansen – 2017-07-25T08:43:34.547

1save 4 bytes with g=lambda x,d=0:x**(d>99or g(x,d+1)), x**True evaluates to x – Uriel – 2017-07-25T12:57:28.143

@Uriel, That is really smart..... Thanks!!! – officialaimm – 2017-07-25T13:09:37.270

6

Python 3, 37 30 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!

daniero

Posted 2017-07-25T07:09:46.497

Reputation: 17 193

1

FYI, it appears that f-strings were first introduced in Python 3.6: see https://www.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 – 2017-07-25T16:01:38.213

1You don't need to substitute in the value of x, eval('x**'*99+'1') works – xnor – 2017-07-25T18:51:42.540

@xnor doh, of course it does :) thanks – daniero – 2017-07-25T19:08:19.173

@xnor Neat -- I applied the same thing in my Ruby answer and it somehow fixed it :) – daniero – 2017-07-25T19:17:37.257

1+1, I am slapping myself for forgetting the existence of eval.... :D – officialaimm – 2017-07-26T05:29:01.687

4

Mathematica, 12 bytes

#//.x_:>#^x&

Takes a floating‐point number as input.

alephalpha

Posted 2017-07-25T07:09:46.497

Reputation: 23 988

4

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

miles

Posted 2017-07-25T07:09:46.497

Reputation: 15 654

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 – 2017-07-25T14:30:37.177

1@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 – 2017-07-25T14:36:58.643

4

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.

Charlie

Posted 2017-07-25T07:09:46.497

Reputation: 11 448

1+1 For the complex class I didn't even know that existed. – TheLethalCoder – 2017-07-25T11:29:28.797

1@TheLethalCoder neither did I until I googled it. :-) – Charlie – 2017-07-25T11:30:57.630

3

Jelly, 5 bytes

³*$ÐL

Try it online!

Dennis

Posted 2017-07-25T07:09:46.497

Reputation: 196 637

2

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!

daniero

Posted 2017-07-25T07:09:46.497

Reputation: 17 193

Take out the space after the eval. – Value Ink – 2017-07-25T21:41:01.300

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 – 2017-07-26T00:39:54.700

@Ø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 – 2017-07-26T02:37:28.437

I thought eval use was forbidden. – V. Courtois – 2017-07-26T09:07:12.420

@V.Courtois Ok. But it's not. – daniero – 2017-07-26T10:10:39.797

2

TI-BASIC, 16 bytes

The input and output are stored in Ans.

Ans→X
While Ans≠X^Ans
X^Ans
End

kamoroso94

Posted 2017-07-25T07:09:46.497

Reputation: 739

1

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)

Giuseppe

Posted 2017-07-25T07:09:46.497

Reputation: 21 077

1scan(,1i) works. Similar to how scan(,'') works. – JAD – 2017-07-25T19:20:57.690

@JarkoDubbeldam of course! sometimes my brain doesn't work. – Giuseppe – 2017-07-25T19:24:48.453

1

Javascript, 33 bytes

f=(x,y=x)=>(x**y===y)?y:f(x,x**y)

quant2016

Posted 2017-07-25T07:09:46.497

Reputation: 11

JavaScript doesn't handle imaginary numbers. – kamoroso94 – 2017-07-25T22:24:35.457

1

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.

Cinaski

Posted 2017-07-25T07:09:46.497

Reputation: 1 588

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-]&`](https://tio.run/##y00syfn/v6SkJC6hsjyuxNRXN1bt/39DPRMA) and save a few bytes.

– Luis Mendo – 2017-07-27T15:01:16.653

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 – 2017-07-27T15:09:10.463

@LuisMendo Thanks for the great tips! I have a lot to learn about MATL, but I really like it. – Cinaski – 2017-07-28T09:11:29.637

Glad to hear that! – Luis Mendo – 2017-07-28T09:11:51.380

0

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.

Sean

Posted 2017-07-25T07:09:46.497

Reputation: 4 136