Print the tetration

16

1

Tetration, represented as a^^b, is repeated exponentiation. For example, 2^^3 is 2^2^2, which is 16.

Given two numbers a and b, print a^^b.

Test cases

1 2 -> 1
2 2 -> 4
5 2 -> 3125
3 3 -> 7625597484987
etc.

Scientific notation is acceptable.

Remember, this is , so the code with the smallest number of bytes wins.

Oliver Ni

Posted 2016-10-22T00:38:50.750

Reputation: 9 650

2What kind of numbers? Positive integers? – xnor – 2016-10-22T00:42:05.017

Related – acrolith – 2016-10-22T00:43:17.927

9Exponentiation is non-associative. You should include at least one test cade with b > 2. – Dennis – 2016-10-22T01:02:32.843

@Dennis 3 3 -> 7625597484987 – Gabriel Benamy – 2016-10-22T01:12:00.323

Can I get the inputs reversed? – Erik the Outgolfer – 2016-10-23T16:43:27.810

@EriktheGolfer Yes – Oliver Ni – 2016-10-23T20:06:53.683

What are the two downvotes for? – Oliver Ni – 2016-10-24T17:57:24.957

So 3^3^3 is 3^(3^(3)) and not (3^3)^3 you have to put () – RosLuP – 2016-11-17T15:36:34.403

1

@RosLuP No, 3^3^3 automatically means 3^(3^(3)). See https://en.wikipedia.org/wiki/Order_of_operations, where it says "Stacked exponents are applied from the top down, i.e., from right to left."

– Oliver Ni – 2016-11-17T16:10:26.257

Related. – mbomb007 – 2016-12-23T14:47:46.747

Answers

14

Dyalog APL, 3 bytes

*/⍴

TryAPL.

Explanation

*/⍴  Input: b (LHS), a (RHS)
  ⍴  Create b copies of a
*/   Reduce from right-to-left using exponentation

miles

Posted 2016-10-22T00:38:50.750

Reputation: 15 654

1Hey, somebody who's beating @Dennis! Now that's rare! (; :P – HyperNeutrino – 2016-12-03T22:30:12.420

10

J, 5 4 bytes

^/@#

This is literally the definition of tetration.

Usage

   f =: ^/@#
   3 f 2
16
   2 f 1
1
   2 f 2
4
   2 f 5
3125
   4 f 2
65536

Explanation

^/@#  Input: b (LHS), a (RHS)
   #  Make b copies of a
^/@   Reduce from right-to-left using exponentation

miles

Posted 2016-10-22T00:38:50.750

Reputation: 15 654

Ok a^^b is above reversed b^^a... – RosLuP – 2016-10-22T15:16:21.440

@RosLuP Yes, J and APL evaluate from right-to-left, so 2 ^ 2 ^ 2 is evaluated as 2 ^ (2 ^ 2) and so on – miles – 2016-10-24T10:53:57.567

9

Haskell, 19 bytes

a%b=iterate(a^)1!!b

Iterates exponentiating starting at 1 to produce the list [1,a,a^a,a^a^a,...], then take the b'th element.

Same length directly:

a%0=1;a%b=a^a%(b-1)

Point-free is longer:

(!!).(`iterate`1).(^)

xnor

Posted 2016-10-22T00:38:50.750

Reputation: 115 687

9

Mathematica, 16 bytes

Power@@Table@##&

Explanation

Table@##

Make b copies of a.

Power@@...

Exponentiation.

JungHwan Min

Posted 2016-10-22T00:38:50.750

Reputation: 13 290

8

Python, 30 bytes

f=lambda a,b:b<1or a**f(a,b-1)

Uses the recursive definition.

xnor

Posted 2016-10-22T00:38:50.750

Reputation: 115 687

5

Python, 33 bytes

lambda a,b:eval('**'.join([a]*b))

This evaluates to an unnamed function, that takes the string representation of a number and a number. For example:

>>> f=lambda a,b:eval('**'.join([a]*b))
>>> f('5',2)
3125
>>>

If mixing input formats like this does not count, there is also this 38 byte version:

lambda a,b:eval('**'.join([str(a)]*b))

James

Posted 2016-10-22T00:38:50.750

Reputation: 54 537

2What a cool method! – xnor – 2016-10-22T00:49:47.433

5

Jelly, 4 bytes

x*@/

Try it online! or verify all test cases.

How it works

x*@/  Main link. Arguments: a, b

x     Repeat [a] b times.
 *@/  Reduce the resulting array by exponentation with swapped arguments.

Dennis

Posted 2016-10-22T00:38:50.750

Reputation: 196 637

3

Perl, 19 bytes

Includes +1 for -p

Give numbers on separate lines on STDIN

tetration.pl
2
3
^D

tetration.pl

#!/usr/bin/perl -p
$_=eval"$_**"x<>.1

Ton Hospel

Posted 2016-10-22T00:38:50.750

Reputation: 14 114

3

R, 39 bytes

Recursive function:

f=function(a,b)ifelse(b>0,a^f(a,b-1),1)

Billywob

Posted 2016-10-22T00:38:50.750

Reputation: 3 363

2

Element, 11 bytes

__2:':1[^]`

Try it online!

This is just "straightforward" exponentiation in a loop.

__2:':1[^]`
__              take two values as input (x and y)
  2:'           duplicate y and send one copy to the control stack
     :          make y copies of x
      1         push 1 as the initial value
       [ ]      loop y times
        ^       exponentiate
          `     print result

PhiNotPi

Posted 2016-10-22T00:38:50.750

Reputation: 26 739

2

JavaScript (ES7), 24 bytes

f=(a,b)=>b?a**f(a,b-1):1

The ES6 version is 33 bytes:

f=(a,b)=>b?Math.pow(a,f(a,b-1)):1

ETHproductions

Posted 2016-10-22T00:38:50.750

Reputation: 47 880

Save 1 byte: f=a=>b=>b?a**f(a,b-1):1 – programmer5000 – 2017-04-06T18:42:08.417

2

dc, 35 29 bytes:

?dsdsa?[ldla^sa1-d1<b]dsbxlap

Here is my first complete program in dc.

R. Kap

Posted 2016-10-22T00:38:50.750

Reputation: 4 730

1

Perl, 40 bytes

map{$a=$ARGV[0]**$a}0..$ARGV[1];print$a;

Accepts two integers as input to the function and outputs the result

Gabriel Benamy

Posted 2016-10-22T00:38:50.750

Reputation: 2 827

1Use pop to get $ARGV[1], then use "@ARGV" to get $ARGV[0]. Use say instead of print (option -M5.010 or -E is free). But still, ARGV is terribly long. A -p program almost always wins – Ton Hospel – 2016-10-22T06:56:36.570

1

Actually, 6 bytes

n`ⁿ)`Y

Try it online!

Input is taken as b\na (\n is a newline)

Explanation:

n`ⁿ)`Y
n       a copies of b
 `ⁿ)`Y  while stack changes between each call (fixed-point combinator):
  ⁿ       pow
   )      move top of stack to bottom (for right-associativity)

Mego

Posted 2016-10-22T00:38:50.750

Reputation: 32 998

1

GameMaker Language, 52 50 bytes

d=a=argument0;for(c=1;c<b;c++)d=power(a,d)return d

Timtech

Posted 2016-10-22T00:38:50.750

Reputation: 12 038

This is my 300th answer :o – Timtech – 2016-10-23T20:35:50.020

GameMaker wtf? lol – Simply Beautiful Art – 2017-10-27T23:21:55.887

@SimplyBeautifulArt Yes, and while I'm at it I'll take off 2 bytes for you. – Timtech – 2017-10-28T02:44:59.940

Lol, nice. =) Have my +1, seems simple enough and I understand it. – Simply Beautiful Art – 2017-10-28T19:11:05.893

@SimplyBeautifulArt Appreciated – Timtech – 2017-10-29T03:38:07.680

1

CJam, 9 bytes

q~)*{\#}*

Try it online!

Explanation

q~          e# Take input (array) and evaluate
  )         e# Pull off last element
   *        e# Array with the first element repeated as many times as the second
    {  }*   e# Reduce array by this function
     \#     e# Swap, power

Luis Mendo

Posted 2016-10-22T00:38:50.750

Reputation: 87 464

1

PHP, 51 Bytes

for($b=$p=$argv[1];++$i<$argv[2];)$p=$b**$p;echo$p;

Jörg Hülsermann

Posted 2016-10-22T00:38:50.750

Reputation: 13 026

0

Axiom 70 bytes

l(a,b)==(local i;i:=1;r:=a;repeat(if i>=b then break;r:=a^r;i:=i+1);r)

this less golfed

l(a,b)==
  local i
  i:=1;r:=a;repeat(if i>=b then break;r:=a^r;i:=i+1)
  r


(3) ->  [l(1,2),l(2,2),l(5,2),l(3,3),l(4,3)]

     (3)
     [1, 4, 3125, 7625597484987,
      13407807929942597099574024998205846127479365820592393377723561443721764030_
       0735469768018742981669034276900318581864860508537538828119465699464336490_
       06084096
       ]
                                                   Type: List PositiveInteger

RosLuP

Posted 2016-10-22T00:38:50.750

Reputation: 3 036

0

Bash, 50 bytes

(within the bounds of bash integer data type)

Golfed

E() { echo $(($(printf "$1**%.0s" `seq 1 $2`)1));}

Explanation

Build expression with printf, e.g. E 2 5:

  2**2**2**2**2**1

then use bash built-in arithmetic expansion to compute the result

Test

E 1 2
1

E 2 2
4

E 5 2
3125

E 3 3
7625597484987

zeppelin

Posted 2016-10-22T00:38:50.750

Reputation: 7 884

0

Powershell, 68 Bytes

filter p ($a){[math]::Pow($a,$_)};iex (,$args[0]*$args[1]-join"|p ")

This is the shortest of the three approaches I tried, not that great overall though, i'm 100% sure there's a shorter approach but the few things I tried somehow ended up with slightly more bytes.

PS C:\++\golf> (1,2),(2,2),(5,2),(3,3) | % {.\sqsq $_[0] $_[1]}
1
4
3125
7625597484987

Sadly Powershell has no built-in ^ or ** operator, or it would be a clean 32/33 byte answer, i.e.

iex (,$args[0]*$args[1]-join"^")

colsw

Posted 2016-10-22T00:38:50.750

Reputation: 3 195

0

Wonder, 21 bytes

f\@@[#0?^#1f#1-#0 1?1

Uses the recursive approach. Usage:

f\@@[#0?^#1f#1-#0 1?1];f 2 3

Bonus solution, 22 bytes

@@:^ -#0 1(genc ^#1)#1

A slightly unconventional approach. Usage:

t\@@+>#[^;#1]tk -#0 1rpt#1;t 2 3

More readable:

@@
  iget
    - #0 1
    (genc ^#1) #1

Assuming a^^b:

Generates an infinite list of tetrated a; for a=2, this list would look something like [2 4 16 65536...]. Then indexes at b-1 because Wonder is zero-indexed.

Mama Fun Roll

Posted 2016-10-22T00:38:50.750

Reputation: 7 234

0

Clojure, 56 bytes

(fn[a b](last(take a(iterate #(apply *(repeat % b))b))))

Maybe there is a shorter way via apply comp?

NikoNyrh

Posted 2016-10-22T00:38:50.750

Reputation: 2 361

0

Funky, 27 bytes

f=(a,b)=>(b<2)?a:a^f(a,b-1)

Try it online!

ATaco

Posted 2016-10-22T00:38:50.750

Reputation: 7 898

0

Pyth, 6 bytes

u^QGE1

Try it online.

Explanation

          (implicit: input a to Q)
     1    Start from 1.
u   E     b times,
 ^GQ      raise the previous number to power a.

PurkkaKoodari

Posted 2016-10-22T00:38:50.750

Reputation: 16 699

0

Minkolang 0.15, 12 11 bytes

nnDI1-[;]N.

Try it here!

Explanation

nn             Read two integers from input
  D            Pop top of stack and duplicate next element that many times
   I1-         Push length of stack, minus 1
      [        Pop top of stack and repeat for loop that many times
       ;       Pop b, a and push a^b
        ]      Close for loop
         N.    Output as number and stop.

El'endia Starman

Posted 2016-10-22T00:38:50.750

Reputation: 14 504

0

Racket 51 bytes

(define ans 1)(for((i b))(set! ans(expt a ans)))ans

Ungolfed:

(define (f a b)
  (define ans 1)
  (for((i b))
    (set! ans
          (expt a ans)))
  ans)

Testing:

(f 1 2)
(f 2 2)
(f 5 2)
(f 3 3)

Output:

1
4
3125
7625597484987

rnso

Posted 2016-10-22T00:38:50.750

Reputation: 1 635

0

Scala, 45 bytes

Seq.fill(_:Int)(_:Double)reduceRight math.pow

Ungolfed:

(a:Int,b:Double)=>Seq.fill(a)(b).reduceRight(math.pow)

Build a sequence of as with b elements, and apply math.pow from right to left.

corvus_192

Posted 2016-10-22T00:38:50.750

Reputation: 1 889

0

TI-Basic, 19 bytes

Prompt A,B
A
For(C,2,B
A^Ans
End

Timtech

Posted 2016-10-22T00:38:50.750

Reputation: 12 038

0

Java 7, 71 57 bytes

double c(int a,int b){return b>0?Math.pow(a,c(a,b-1)):1;}

Ungolfed & test code:

Try it here.

class M{
  static double c(int a, int b){
    return b > 0
            ? Math.pow(a, c(a, b-1))
            :1;
  }

  public static void main(String[] a){
    System.out.println(c(1, 2));
    System.out.println(c(2, 2));
    System.out.println(c(5, 2));
    System.out.println(c(3, 3));
  }
}

Output:

1.0
4.0
3125.0
7.625597484987E12

Kevin Cruijssen

Posted 2016-10-22T00:38:50.750

Reputation: 67 575

0

C, 50 bytes

double t(int x,int n){return n?pow(x,t(x,n-1)):1;}

Straightforward from the definition of Tetration.

Karl Napf

Posted 2016-10-22T00:38:50.750

Reputation: 4 131

0

05AB1E, 4 bytes

sF¹m

Try it online!

s     # Swap input arguments.
 F    # N times...
  ¹m  # Top of the stack ^ the first argument.

3 bytes if arguments can be swapped:

F¹m

Magic Octopus Urn

Posted 2016-10-22T00:38:50.750

Reputation: 19 422

2 2 result 16 not 4=2^2 – RosLuP – 2016-11-17T15:32:59.363

a=5, b=2 should output 3125. I'm not sure what order you're taking the input in , but however I put in 5 and 2 I get the wrong result. – FlipTack – 2016-11-18T13:02:10.647