| Bytes | Lang | Time | Link |
|---|---|---|---|
| 029 | AWK | 241202T192840Z | xrs |
| 016 | Juby | 240921T191255Z | Jordan |
| 003 | Uiua | 240921T172747Z | nyxbird |
| 016 | Raku | 230813T102637Z | Sean |
| 003 | Thunno 2 t | 230813T094225Z | The Thon |
| 037 | Red | 210604T143851Z | Aaroneou |
| 007 | K ngn/k | 210609T061601Z | Bubbler |
| 026 | Factor | 210529T224415Z | chunes |
| 004 | Vyxal r | 210530T222438Z | user |
| 002 | 05AB1E | 200923T001212Z | Makonede |
| 009 | GolfScript | 200922T215047Z | 2014MELO |
| 014 | Flurry nii | 200824T043807Z | Bubbler |
| 027 | Funky | 171029T221925Z | ATaco |
| 050 | GameMaker Language | 161022T150926Z | Timtech |
| 056 | Clojure | 161223T132928Z | NikoNyrh |
| 021 | Wonder | 161203T221304Z | Mama Fun |
| 070 | Axiom | 161118T082841Z | user5898 |
| 068 | Powershell | 161118T164949Z | colsw |
| 050 | Bash | 161118T105728Z | zeppelin |
| 050 | C | 161114T010226Z | Karl Nap |
| 057 | Java 7 | 161025T090924Z | Kevin Cr |
| 019 | TIBasic | 161022T150558Z | Timtech |
| 051 | PHP | 161022T175758Z | Jör |
| 009 | CJam | 161022T151147Z | Luis Men |
| 039 | R | 161022T141821Z | Billywob |
| 045 | Scala | 161022T124057Z | corvus_1 |
| 019 | Perl | 161022T065457Z | Ton Hosp |
| 006 | Actually | 161022T051448Z | user4594 |
| 051 | Racket | 161022T044553Z | rnso |
| 029 | dc | 161022T025645Z | R. Kap |
| 004 | Jelly | 161022T011042Z | Dennis |
| 016 | Mathematica | 161022T030306Z | JungHwan |
| 011 | Minkolang 0.15 | 161022T013419Z | El'e |
| 033 | Python | 161022T004713Z | DJMcMayh |
| 006 | Pyth | 161022T011902Z | PurkkaKo |
| 024 | JavaScript ES7 | 161022T011902Z | ETHprodu |
| 003 | Dyalog APL | 161022T011744Z | miles |
| 004 | J | 161022T010155Z | miles |
| 011 | Element | 161022T010324Z | PhiNotPi |
| 040 | Perl | 161022T010231Z | Gabriel |
| 019 | Haskell | 161022T004758Z | xnor |
| 030 | Python | 161022T004925Z | xnor |
Uiua, 3 bytes
/ⁿ↯
Takes reversed inputs (appending : flip removes this).
↯ reshape a into length b, and / reduce with ⁿ power.
Raku, 16 bytes
{[**] $^a xx$^b}
$^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.
Thunno 2 t, 3 bytes
ọƲ@
Explanation
ọƲ@ # Implicit input
ọ # Repeat [a] b times
Ʋ # Reduce the list by:
@ # Swapped exponentiation
# Implicit output
Red, 38 26 37 bytes
Thanks to @9214 for telling me about the loop command for -12 bytes.
+11 bytes to fix invalidities brought out by @Bubbler.
func[a b][x: a loop b - 1[x: a ** x]]
K (ngn/k), 7 bytes
(*/#)/#
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.
Vyxal r, 4 bytes
This is lyxal's solution
‹(⁰e
‹(⁰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ḭ
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
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
GolfScript, 9 bytes
~])*{\?}*
~] # 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).
Flurry -nii, 14 bytes
{}{{}({})}{{}}
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
GameMaker Language, 52 50 bytes
d=a=argument0;for(c=1;c<b;c++)d=power(a,d)return d
Clojure, 56 bytes
(fn[a b](last(take a(iterate #(apply *(repeat % b))b))))
Maybe there is a shorter way via apply comp?
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.
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
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"^")
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
C, 50 bytes
double t(int x,int n){return n?pow(x,t(x,n-1)):1;}
Straightforward from the definition of Tetration.
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:
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
TI-Basic, 19 bytes
Prompt A,B
A
For(C,2,B
A^Ans
End
PHP, 51 Bytes
for($b=$p=$argv[1];++$i<$argv[2];)$p=$b**$p;echo$p;
CJam, 9 bytes
q~)*{\#}*
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
R, 39 bytes
Recursive function:
f=function(a,b)ifelse(b>0,a^f(a,b-1),1)
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.
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
Actually, 6 bytes
n`ⁿ)`Y
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)
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
dc, 35 29 bytes:
?dsdsa?[ldla^sa1-d1<b]dsbxlap
Here is my first complete program in dc.
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.
Mathematica, 16 bytes
Power@@Table@##&
Explanation
Table@##
Make b copies of a.
Power@@...
Exponentiation.
Minkolang 0.15, 12 11 bytes
nnDI1-[;]N.
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.
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))
Pyth, 6 bytes
u^QGE1
Explanation
(implicit: input a to Q)
1 Start from 1.
u E b times,
^GQ raise the previous number to power a.
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
Dyalog APL, 3 bytes
*/⍴
Explanation
*/⍴ Input: b (LHS), a (RHS)
⍴ Create b copies of a
*/ Reduce from right-to-left using exponentation
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
Element, 11 bytes
__2:':1[^]`
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
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
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).(^)
Python, 30 bytes
f=lambda a,b:b<1or a**f(a,b-1)
Uses the recursive definition.