18
\$\begingroup\$

Tetration, represented as \${}^ba\$, is repeated exponentiation. For example, \${}^32\$ is \$2^{2^2}\$, which is \$16\$.

Given two numbers \$a\$ and \$b\$, print \${}^ba\$.

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.

\$\endgroup\$
10
  • 3
    \$\begingroup\$ What kind of numbers? Positive integers? \$\endgroup\$ – xnor Oct 22 '16 at 0:42
  • 1
    \$\begingroup\$ Related \$\endgroup\$ – acrolith Oct 22 '16 at 0:43
  • 12
    \$\begingroup\$ Exponentiation is non-associative. You should include at least one test cade with b > 2. \$\endgroup\$ – Dennis Oct 22 '16 at 1:02
  • 1
    \$\begingroup\$ @Dennis 3 3 -> 7625597484987 \$\endgroup\$ – Gabriel Benamy Oct 22 '16 at 1:12
  • 3
    \$\begingroup\$ @RosLuP No, 3^3^3 automatically means 3^(3^(3)). See en.wikipedia.org/wiki/Order_of_operations, where it says "Stacked exponents are applied from the top down, i.e., from right to left." \$\endgroup\$ – Oliver Ni Nov 17 '16 at 16:10

37 Answers 37

16
\$\begingroup\$

Dyalog APL, 3 bytes

*/⍴

TryAPL.

Explanation

*/⍴  Input: b (LHS), a (RHS)
  ⍴  Create b copies of a
*/   Reduce from right-to-left using exponentation
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hey, somebody who's beating @Dennis! Now that's rare! (; :P \$\endgroup\$ – hyper-neutrino Dec 3 '16 at 22:30
10
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Ok a^^b is above reversed b^^a... \$\endgroup\$ – user58988 Oct 22 '16 at 15:16
  • \$\begingroup\$ @RosLuP Yes, J and APL evaluate from right-to-left, so 2 ^ 2 ^ 2 is evaluated as 2 ^ (2 ^ 2) and so on \$\endgroup\$ – miles Oct 24 '16 at 10:53
9
\$\begingroup\$

Python, 30 bytes

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

Uses the recursive definition.

\$\endgroup\$
0
9
\$\begingroup\$

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).(^)
\$\endgroup\$
9
\$\begingroup\$

Mathematica, 16 bytes

Power@@Table@##&

Explanation

Table@##

Make b copies of a.

Power@@...

Exponentiation.

\$\endgroup\$
6
\$\begingroup\$

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.
\$\endgroup\$
0
5
\$\begingroup\$

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))
\$\endgroup\$
1
  • 2
    \$\begingroup\$ What a cool method! \$\endgroup\$ – xnor Oct 22 '16 at 0:49
4
\$\begingroup\$

05AB1E, 9 4 2 bytes

-2 thanks to ovs.

Gm

Try it online! Beats all other answers

Gm  # full program
G   # for N in [1, 2, 3, ...,
    # ..., implicit input...
G   # ...minus 1]...
 m  # push...
    # implicit input...
 m  # to the power of...
    # implicit input...
    # (implicit) or top of stack if not first iteration
    # implicit output
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Welcome to the site and impressive first answer! I've edited your TIO link so that it includes an example input. Be sure to check out other recently active challenges as well as our Tips for golfing in 05AB1E page to see if you can shave any bytes off your answer! \$\endgroup\$ – caird coinheringaahing Sep 23 '20 at 0:28
  • \$\begingroup\$ Thanks! I did actually end up checking out that page before submitting this answer :) \$\endgroup\$ – Makonede Sep 23 '20 at 0:33
  • 1
    \$\begingroup\$ Does Gm work? \$\endgroup\$ – ovs May 29 at 21:47
  • \$\begingroup\$ @ovs (-‸ლ) of course it does. Thank you! \$\endgroup\$ – Makonede May 29 at 21:49
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

R, 39 bytes

Recursive function:

f=function(a,b)ifelse(b>0,a^f(a,b-1),1)
\$\endgroup\$
3
\$\begingroup\$

Factor, 29 26 bytes

[ 1 rot [ dupd ^ ] times ]

Try it online!

-3 thanks to @Bubbler

\$\endgroup\$
1
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ Save 1 byte: f=a=>b=>b?a**f(a,b-1):1 \$\endgroup\$ – user58826 Apr 6 '17 at 18:42
  • \$\begingroup\$ @programmer5000 no, the function call would have to be f(a)(b-1) inside, adding 1 byte and cancelling out the effect. \$\endgroup\$ – Recursive Co. Jun 1 at 7:52
  • \$\begingroup\$ @ophact You're right, but that's a 4-year-old comment... However, you can really get -3 bytes by calling only the inner function. \$\endgroup\$ – FZs Jun 5 at 19:11
2
\$\begingroup\$

dc, 35 29 bytes:

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

Here is my first complete program in dc.

\$\endgroup\$
2
\$\begingroup\$

PHP, 51 Bytes

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

GameMaker Language, 52 50 bytes

d=a=argument0;for(c=1;c<b;c++)d=power(a,d)return d
\$\endgroup\$
5
  • \$\begingroup\$ This is my 300th answer :o \$\endgroup\$ – Timtech Oct 23 '16 at 20:35
  • \$\begingroup\$ GameMaker wtf? lol \$\endgroup\$ – Simply Beautiful Art Oct 27 '17 at 23:21
  • \$\begingroup\$ @SimplyBeautifulArt Yes, and while I'm at it I'll take off 2 bytes for you. \$\endgroup\$ – Timtech Oct 28 '17 at 2:44
  • \$\begingroup\$ Lol, nice. =) Have my +1, seems simple enough and I understand it. \$\endgroup\$ – Simply Beautiful Art Oct 28 '17 at 19:11
  • \$\begingroup\$ @SimplyBeautifulArt Appreciated \$\endgroup\$ – Timtech Oct 29 '17 at 3:38
1
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Use 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 \$\endgroup\$ – Ton Hospel Oct 22 '16 at 6:56
1
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Flurry -nii, 14 bytes

{}{{}({})}{{}}

Try it online!

Takes two numbers a b from stdin, and outputs as the return value.

How it works

It works basically like xnor's Haskell answer: repeat (a^) b times to 1. a is left on the stack, so that it can be referenced in each iteration.

// In Church numeral, x a → a^x
main = b power_of_a 1

power_of_a = \x. x a
           = \x. x (push pop)  // assumes a is on the top of the stack,
                               // pops and takes its value and pushes it back
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 9 bytes

~])*{\?}*

Try it online!

~]          # Put a and b in an array
  )         # Remove b from the array
   *        # Make b copies of a
    {  }*   # Execute this block for each number in the array
     \?     # Exponentiation

Just {?} would be (3^3)^3 instead of 3^(3^3).

\$\endgroup\$
1
+100
\$\begingroup\$

Vyxal r, 4 bytes

This is lyxal's solution

‹(⁰e

Try it Online!

‹(⁰e
‹     Decrement b
 (  ) For loop (second parentheses is implicit) using b-1
  ⁰   Push a
   e  And raise it to the power of the accumulator (initially a)

Vyxal, 6 5 bytes

Saved 1 byte thanks to lyxal

ʁ•⁽eḭ

Try it Online!

This one is a worse solution. a is inputted as a singleton list.

ʁ•λe;ḭ
ʁ      Make a range [0,b) (only the length of the range matters (b))
 •     Reshape [a] to that (make a list of b a's)
     ḭ Reduce from the right
  λe;  using exponentiation
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 5 bytes \$\endgroup\$ – lyxal May 30 at 22:27
  • \$\begingroup\$ 4 bytes \$\endgroup\$ – lyxal May 30 at 22:53
1
\$\begingroup\$

K (ngn/k), 7 bytes

(*/#)/#

Try it online!

Takes two inputs in the order of [hyperexponent; base] and returns the result.

K does not have exponentiation built-in, so I simulate n^m with "product */ of m copies # of n" instead. Tetration is basically the same, in the sense that it is a fold by exponentiation. K's fold is left to right, and */# accepts [exponent; base], so the direction of exponentiation is correctly y^(y^(y^...)).

Although it does not use exponentiation built-in, it fails to solve this challenge because 4^(4^4) is way too big to handle with built-in integer sizes.

\$\endgroup\$
0
\$\begingroup\$

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.
        
\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 19 bytes

Prompt A,B
A
For(C,2,B
A^Ans
End
\$\endgroup\$
0
0
\$\begingroup\$

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.