20
\$\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\$ Commented Oct 22, 2016 at 0:42
  • 1
    \$\begingroup\$ Related \$\endgroup\$ Commented Oct 22, 2016 at 0:43
  • 13
    \$\begingroup\$ Exponentiation is non-associative. You should include at least one test cade with b > 2. \$\endgroup\$ Commented Oct 22, 2016 at 1:02
  • 1
    \$\begingroup\$ @Dennis 3 3 -> 7625597484987 \$\endgroup\$ Commented Oct 22, 2016 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\$ Commented Nov 17, 2016 at 16:10

42 Answers 42

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

C, 50 bytes

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

Straightforward from the definition of Tetration.

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

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

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"^")

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

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 
\$\endgroup\$
1
  • \$\begingroup\$ 8 bytes would be reduced if local i; could be removed. \$\endgroup\$ Commented May 29, 2021 at 23:34
0
\$\begingroup\$

Clojure, 56 bytes

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

Maybe there is a shorter way via apply comp?

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

Funky, 27 bytes

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

Try it online!

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

Thunno 2 t, 3 bytes

ọƲ@ 

Try it online!

Explanation

ọƲ@ # Implicit input ọ # Repeat [a] b times Ʋ # Reduce the list by: @ # Swapped exponentiation # Implicit output 
\$\endgroup\$
0
\$\begingroup\$

Raku, 16 bytes

{[**] $^a xx$^b} 

Try it online!

$^a and $^b are the arguments to this anonymous function. $^a xx $^b is a list of the first argument replicated a number of times given by the second argument. [**] reduces that list with the exponentiation operator. The reduction respects the right-associativity of the operator.

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

J-uby, 16 bytes

Takes reversed arguments.

~:*%-[I]|:/&~:** 

Attempt This Online!

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

AWK, 29 bytes

{for(x=$1;++i<$2;)x=$1^x}$0=x 

Attempt This Online!

\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.