# Implement hyperexponentiation/tetration without the use of '^' [closed]

## The Challenge

Implement tetration (aka Power Tower or Hyperexponentiation) with the least amount of characters.

## The Conditions

• Don't use the 'power' operator or its equivalents (such as pow(x,y), x^y, x**y, etc.)
• Input given as: x y (separated by a space)
• x is exponentiated by itself y times.
• Your method must be able to compute at least 4 3 (4 exponentiated by itself 3 times)

## The Scoring

• Lowest score wins: (# of characters)
• Bonus deduction if you do not use the multiplication operator (-5 points).
• No Speed/Memory requirements. Take as long as you want.

## Examples

x, 0 -> 1

2, 2 -> 2^2 = 4

2, 4 -> 2^(2^(2^2)) = 65536

4, 3 -> 4^(4^4) = 4^256 = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096


Open to suggestions/alterations/questions

• One alteration which I think is fairly important is to replace "* operator" with "multiplication operator". In GolfScript * is multiplication in some contexts, but it's also the simple looping operator: {block}N* is equivalent to C-style for(i=0;i<N;i++){block}. The tricky edge case is string/array multiplication ('a'3* gives 'aaa'), but that's unlikely to be an issue given that an array of 4***3 elements will overflow RAM. Commented Apr 19, 2012 at 8:38
• Also worth adding a test for the edge case x 0 => 1. My original solution didn't handle that case. Commented Apr 19, 2012 at 21:20
• The penalty for using multiplication is way too low. (:=bonus for not using it). I made a solution which didn't used it, and had to replace it to avoid stack overflows, and gained a 7 char win for a 5 char bonus loss. Commented Apr 20, 2012 at 3:32
• @EngineerToast I posted this golf 4 years before the one you linked... Commented Sep 19, 2017 at 23:13
• The conditions and scoring are kind of strange. You don't allow the use of power operations? Or you do allow them, but they are a +10 point bonus? Commented Dec 4, 2017 at 23:58

# J, score is 7 (12 chars - 5 points for avoiding multiplication)

+/@$/@$~/@$~ usage:  4 +/@$/@$~/@$~ 3
1.34078e154
t=.+/@$/@$~/@$~ NB. define a function 4 t 3 1.34078e154 2 t 2 4  Just few nested folds: • Using multiplication it would be */@$~/@$~ • Using power it would be ^/@$~ where $~ creates array, / is a fold function. • Nicely done. (pad) Commented Aug 15, 2012 at 10:20 • @Gareth Thanks, but was does pad mean here? Sorry, English is not my mother tongue. Commented Aug 15, 2012 at 10:25 • My message was too short so I needed to pad it out. :-) Commented Aug 15, 2012 at 10:30 • Could you get pentation just by providing one more @$~ in the conjunction? Commented Sep 19, 2017 at 23:03
• @Jonah You'd need the /, but yes. you're just folding as many times as needed over the nested folding function. Commented Oct 22, 2017 at 20:44

## Haskell, 87 85 - 5 == 80 82

import Data.List
t x=genericLength.(iterate(sequence.map(const$replicate x[]))[[]]!!)  Uses neither of exponentiation, multiplication or addition (!), just list operations. Demonstration: Prelude> :m +Data.List Prelude Data.List> let t x=genericLength.(iterate(sequence.map(const$replicate x[]))[[]]!!)
Prelude Data.List> t 2 2
4
Prelude Data.List> t 2 4
65536
Prelude Data.List> t 4 3


...
ahm... you didn't say anything about performance or memory, did you? But given enough billions of years and some petabytes of RAM, this would still yield the correct result (genericLength can use a bigInt to count the length of the list).

• I trust you will have an answer for me by 3012? ;) Commented Apr 19, 2012 at 21:26
• I'll need some help from Moore's law, but that given I might. Commented Apr 19, 2012 at 22:00

## GolfScript, 15 18 chars

~])*1\+{[]+*{*}*}*


Yes, one of the *s is a multiplication operator (exercise: which one?) so I don't qualify for the 5 char bonus. Still, it's just barely shorter than Peter's solution.

This earlier 15-char version is otherwise the same, but produces no output when the second argument is 0. Thanks to r.e.s. for spotting the bug.

~])*{[]+*{*}*}*

• This produces fatal errors, e.g. with "2 3" ~])*{[]+*{*}*}*. Commented Apr 20, 2012 at 3:37
• @r.e.s., it produces the correct answer for me. Commented Apr 20, 2012 at 6:29
• @r.e.s.: It assumes that there's nothing else on the stack but the input. If you want to provide the input in-line like in your example, first use ; to remove the actual input string that the interpreter puts on the stack at start-up. Or just prepend a [ to the code: both ;"2 3" ~])*{[]+*{*}*}* and "2 3" [~])*{[]+*{*}*}* work fine for me. Commented Apr 20, 2012 at 10:40
• (+1) Thanks! Those variations work and solve a mystery for me. The tutorial says "You don't have to pipe input in, but if you don't, it will not prompt for input, instead it will assume there is no input." So I've been using just ruby golfscript.rb my_script.gs on the command line, without knowing that it causes something ("", apparently) to be on the stack before the script is run -- which sometimes works, sometimes not. (Also, with echo 2 3 | ruby golfscript.rb my_script.gs, your program does work as-given.) Commented Apr 20, 2012 at 13:18
• ideone.com/547LG Commented Apr 20, 2012 at 16:01

## J, 1619 12 characters

*/@$~/1,~$~/


or as a verb (17 characters):

h=:[:*/@$~/1,~$~/


usage:

   h 2 4
65536


or taking input from keyboard (24 27 20 characters):

*/@$~/1,~$~/".1!:1]1


with thanks to FUZxxl for pointing out my stupidity. :-)

Explanation:

J is read from right to left, so using 2 4:

/ is used to insert the verb $~ between each pair of items in the list. $~ takes the left item and shapes it $ using the right item (the ~ reverses the arguments) - so this would be equivalent to 4$ 2 which gives you a list of 2s which is four items long 2 2 2 2.

Now we append 1 to the list 1,~ and then do the same thing again; / insert a verb */@$~ between each pair of items in the list. This verb starts in the same way $~ but this time it / inserts a * between each item of the newly generated list. The @ just makes sure that the */@$~ works as one verb instead of two. This gives 2 multiplied by itself enough times to be equivalent to 2^4. J's vocabulary page - I find solving problems with J fun just because of the different way it sometimes does things. Adding one further iteration to remove the * operator has 2 problems • It comes out at 17 characters (+/@$~/,@$~/1,~$~/) which, even with the -5 bonus, is too long
• It runs out of memory if the number gets too large so doesn't meet the requirement of being able to calculate 4 3
• Could you provide an explanation? This looks interesting. Commented Apr 19, 2012 at 21:23
• @MrZander I've edited my answer to add an explanation. Commented Apr 19, 2012 at 21:48
• Not sure if I have a better understanding or more confusion, but thanks haha. Commented Apr 19, 2012 at 21:50
• The explanation implies that the whole thing is doing exponentiation rather than tetration. Which of us is missing something? Commented Apr 19, 2012 at 22:28
$y>1&&t($x,$y-1,eval$x."*$x"x($z-1||1))||$z }  usage: print t(2,4,1)  • an incorrect one too Commented Apr 19, 2012 at 16:19 ## Mathematica, 40 33 This doesn't quite conform to the rules but it is not in contention for the shortest code anyway, and I hope that it will be of interest to someone. m@f_:=Fold[f,1,#2~Table~{#}]&; m[m@Sum]  This builds a "tetration" function when it is run, but the arguments must be given in reverse order. Example: m[m@Sum][3, 4]  1340780792994259709957402499820584612747936582059239337772356144372176 4030073546976801874298166903427690031858186486050853753882811946569946 433649006084096 • Would you explain the code? Or display results on symbols rather than numbers? I notice that Fold[g, 1, #2~Table~{#}] &[3, 4] will produce g[g[g[1, 4], 4], 4] for instance. Commented Aug 9, 2012 at 10:27 • @David m[Times] produces Fold[Times, 1, Table[#2, {#1}]] &, which is a power function: m[Times][5, x] ---> x^5; the same method is used for this new power function to produce a tetration function. Logically one could start with Plus but that fails almost immediately. Commented Aug 9, 2012 at 10:37 • To eliminate Times, try this: t[h_, n_] := Sum[h, {i, n}]. Then run m[m@t][3, 4]. Commented Aug 9, 2012 at 10:46 • @David, yes, that should work, but not for Code-Golf. ;-) (BTW you could write Sum[h, n].) Commented Aug 9, 2012 at 10:47 • Look at the scoring rules. You save 9 points by not using Times. The total score is still not better than yours but getting closer. Commented Aug 9, 2012 at 10:48 ## Haskell: 58 51 chars, with or without multiplication. i f x 1=x;i f x n=f$i f x$n-1 t=i(\o n->i(o n)n)(+)4  Ungolfed: bump op n a = iterate (op n) n !! (fromIntegral$ a-1)
tetrate = iterate bump (+) !! 3


Shorter definition comes from inlining “bump”, and defining a custom version of “iterate”. Unfortunately the result is impossibly inefficient, but starting with (*) instead of (+) gives decent speed. In ghci:

Prelude> let i f x 1=x;i f x n=f$i f x$n-1
(0.00 secs, 1564024 bytes)
Prelude> let t=i(\o n->i(o n)n)(*)3
(0.00 secs, 1076200 bytes)
Prelude> t 4 3
13407807929942597099574024998205846127479365820592393377723561443721764030073546
976801874298166903427690031858186486050853753882811946569946433649006084096
(0.01 secs, 1081720 bytes)


## Ruby 66 59 characters

def e(x,y)
r=1
(1..y).each{t=x
(2..r).each{t*=x}
r=t}
r
end

• Unfortunately, this script does not produce the correct output (1) when the second input number is 0; rather, e(x,0) returns the value of x. Commented Apr 21, 2012 at 3:09
• @r.e.s. you are right. I fixed the code. Thanks! Commented Apr 21, 2012 at 7:31

## Rockstar, 233 bytes

Rockstar is a quite new computer programming language, designed for creating programs that are also song lyrics. Rockstar is heavily influenced by the lyrical conventions of 1980s hard rock and power ballads.

The solution takes two inputs, one per each line (string manipulation is very limited). It uses multiplication operator, so no bonus to apply for :/

P takes A,B
N is 1
C is 1
While C is as low as B
Let N be N of A
Build C up

Give back N

T takes E,F
If F is empty
Give back 1
Else
Knock F down
Let G be T taking E,F
Give back P taking E,G

Listen to X
Listen to Y
Say T taking X,Y


Ungolfed, here's a more lyric version (comments are in parentheses, which are standard comments in Rockstar). Variable names may take up more words, and all words count ('my power' is different from 'the power' and from 'Power')

Power takes the force and the cross          (function def, two parameters: 'the force', 'the cross')
Build my arm up                            ('my arm' := 1) (literally: 'my arm'++, but my arm was initialized with 0)
Build my faith up                          ('my faith' := 1)
While my faith is as weak as the cross     (While loop, condition: 'my faith' <= 'the cross')
Let my arm be the force of my arm        ('my arm' := 'the force' * 'my arm' )
Build my faith up                        ('my faith' ++ )
(empty line to terminate While block)
Give back my arm                           (return 'my arm')
(empty line to terminate function block)
Tower takes the force and the enemy          (function def, two parameters: 'the force', 'the enemy')
If the enemy is nowhere                    (If, condition: 'the enemy' == 0) (literally: 'the enemy' == NULL, but NULL is considered 0 in arithmetics)
Build Victory up                         (Victory := 1, this is a bit diffeerent from the golfed version)
Give back Victory                        (return Victory)
Else                                       (else)
Knock the enemy down                     ('the enemy' -- )
Let the defense be Tower taking the force and the enemy ('the defense' := Tower ('the force', 'the enemy'))
Give back Power taking the force and the defense        (return Power ('the force', 'the defense'))
(empty line to terminate If block)
(empty line to terminate Function block)
Listen to us                                 (input: 'us')


# JavaScript (Node.js), 41 bytes

Have some BigInts

a=>g=b=>b--?(P=n=>n--?a*P(n):1n)(g(b)):1n


Try it online!

# Jelly, 9 bytes - 5 (no multiplication) = 4

xxxS¥/¥@/


Try it online!

# Jelly, 6 bytes - 5 (no multiplication, but cheats by using the list product atom) = 1

xxP¥@/


Try it online!

## Python, 112 chars

The numbers should be the 1st and 2nd argument: python this.py 4 3
** operator not used.
* used. It's quite trivial to implement, exactly like **, but costs more than 5 chars.

import sys
p=lambda y:y and x*p(y-1)or 1
t=lambda y:y>1 and p(t(y-1))or x
x,y=map(long,sys.argv[1:])
print t(y)

• How do I use the code to compute 4 3? And, just of curiosity: Have you tried to implement * that way, and to compute 4 3 then? Commented Apr 20, 2012 at 8:29
• @userunknown, Input is by parameters. I added an explanation to the answer. I didn't try to add the * implementation, I believe the recursion depth would be too large for 4 3. Commented Apr 20, 2012 at 12:10

## C, 117105 99 chars

EDIT: Merged the two functions p and r into one, saving some chars.
Of 99 chars, 52 do the actual calculation (including variable definitions). The other 47 are for handling input and output.
BUG: Badly handles powers of 0 (e.g. 0 2). Should find a minimum cost fix. This isn't a bug, I forgot that 0 2 is undefined.

Successfully handles 4 3, and even gives an exact result. However, can be inaccurate for some smaller numbers.
Prints the number with a trailing .000000.

x,y,z;
double R(){return--y?!z?y=R(),R(z=1):x*R():x;}
main(){
scanf("%d%d",&x,&y);
printf("%f\n",R());
}

• Looks like 118 chars to me: ideone.com/9D5SU Commented Apr 19, 2012 at 16:28
• Testing this with 4 3 is only accurate to about 18 places, double doesn't have nearly enough precision to support an exact representation. Commented Apr 19, 2012 at 19:40
• @Sir_Lagsalot, double has more than enough precision for 4^256. It only has one significant digit. Commented Apr 19, 2012 at 20:03
• Ah good point, I wasn't thinking in binary. Does it actually print out the exact value for you? It gets truncated after the first 18 or so decimal digits on my machine, but I'm willing to accept that's system specific. Commented Apr 19, 2012 at 20:15
• @Sir_Lagsalot: See the ideone link I provided. It prints out the whole number. Commented Apr 19, 2012 at 21:03

## Factor, 187 characters

USING: eval io kernel locals math prettyprint sequences ;
IN: g
:: c ( y x o! -- v )
o 0 = [ x y * ] [ o 1 - o!
y x <repetition> 1 [ o c ] reduce ] if ;
contents eval( -- x y ) swap 2 c .


Before golf:

USING: eval io kernel locals math prettyprint sequences ;
IN: script

! Calculate by opcode:
!   0 => x * y, multiplication
!   1 => x ^ y, exponentiation
!   2 => x ^^ y, tetration
:: calculate ( y x opcode! -- value )
opcode 0 = [
x y *
] [
! Decrement the opcode. Tetration is repeated exponentiation,
! and exponentiation is repeated multiplication.
opcode 1 - opcode!

! Do right-associative reduction. The pattern is
!   seq reverse 1 [ swap ^ ] reduce
! but a repetition equals its own reverse, and 'calculate'
y x <repetition> 1 [ opcode calculate ] reduce
] if ;

contents eval( -- x y )         ! Read input.
swap 2 calculate .              ! Calculate tetration. Print result.


I did not remove the multiplication operator *. If I did so, then I would need to add some logic expressing that the sum of an empty sequence is 0, not 1. This extra logic would cost more than the -5 bonus.

Rule breaker, 124 + 10 = 134 characters

USING: eval kernel math.functions prettyprint sequences ;
contents eval( -- x y ) swap <repetition> 1 [ swap ^ ] reduce .


This program has a lower score, but the exponentiation operator ^ breaks the rules. The rules say "(# of characters) + (10 * (# of 'power' operators))", so I applied the +10 penalty. However, the rules also say "Don't use the 'power' operator", so any program taking this penalty does break the rules. Therefore, this program of 134 characters is not a correct answer, and I must present my longer program of 187 characters as my answer.

## Haskell 110 - 5 = 105

Tetration Peano Style. This is the most insanely slow solution possible, just a warning, but also avoids even addition.

data N=Z|S N
a&+Z=a
a&+S b=S$a&+b _&*Z=Z a&*S b=a&+(a&*b) _&^Z=S Z a&^S b=a&*(a&^b) _&>Z=S Z a&>S b=a&^(a&>b)  This relies on you having the patience to type out Peano numbers (and won't show the answer, If you actually want to run it, add these few lines (90 chars): f 0=Z f a=S$f$a-1 t Z=0 t(S a)=1+t a main=interact$show.f.(\[x,y]->x&>y).map(f.read).words


# Ruby, 47 46 45

t=->x,n{r=x;2.upto(n){r=([x]*r).inject :*};r}

## Lua: 133 chars, multiplication-less

a,b=io.read():match"(%d+) (%d+)"a,b,ba=a+0,b+0,a for i=1,b-1 do o=1 for i=1,a do o=o+o for i=1,ba-b do o=o+o end end a=o end print(o)


I was originally going to use string repetition hacks to do fake multiplication, but it likes to fail on large values. I could possibly use dynamic compilation and loadstring to make it smaller, but it's getting late here... I need sleep.

Entering "4 3" into stdin outputs:

1.3407807929943e+154


# VBA, 90 Chars

*Perhaps the no multiplication bonus is not good enough. I think the no multiplication answer is much more interesting, but this is code golf, so it's not the best. Here's an answer without *, and a better (shorter, and better scoring) answer with it:

## 90 chars, no power operators, uses multiplication = 90

Sub c(x,y)
f=IIf(y,x,1):For l=2 To y:b=x:For j=2 To f:b=b*x:Next:f=b:Next:MsgBox f
End Sub


## 116 chars, no power operators, no multiplication bonus (-5) = 111

Sub c(x,y)
f=IIf(y,x,1):For l=2 To y:b=x:For j=2 To f:For i=1 To x:a=a+b:Next:b=a:a=0:Next:f=b:Next:MsgBox f
End Sub


NOTE: VBA has issues printing the number when the result is very large (i.e. 4, 3), but it does calculate correctly, so if, for example, you wanted to USE that number, you would be good to go. Also, even BIGGER numbers overflow (i.e. 3, 4).

# Perl 6, 32 bytes

->\a,\b{(1,{[*] a xx$_}...*)[b]}  Try it online! (1, { [*] a xx$_ } ... *) is a lazy sequence that generates the power tower, each element being the a list which consists of the first input parameter a replicated (xx) a number of times equal to the previous element (\$_), that list then being reduced with multiplication ([*]). From that sequence we simply pluck out the b-th element.

# Lambda calculus, 10-5

(using Church encoding and De Bruijn indeces)
λλ(1λ13)λ1

## Explanation

Without De Bruijn indeces: λa,b.(b λc.ca)λc.c:

λa,b.                                                 define the anonymous function f(a,b)=
(b                                                apply the following function b times
λc.                                                    the anonymous function g(c)=
ca)                 apply c to a because of church encoding this is equal to a^c
λc.c                              the identity function, 1 in church encoding


If you define exp_a(x)=a^x this program defines a↑↑b=exp_a^b(1) where ^b denotes function itteration.

I'm not sure if this is allowed because ca is technically equivalent to a^c how ever it is not a real built-in and only a side effect of the way integers are encoded in lambda calculus.

• Hm, is there an interpreter so that I can try this? If there's no implementation of a language, then you can't use it to solve challenges here. Languages are based on their implementations here. Commented Mar 9, 2019 at 19:56

Python - 151 bytes

Python 3.0:
r=range
def exp(a,b):
if b=0:
a=1
else:
r1=a
for i in r(1,b):
s=s*a

def tet(a,b):
r1=0
if b=0:
a=1
else:
r1=a
for i in range(1,b):
r1=exp(a, result)


Here's how the new new version works.

1: Exponentiation is initialized (multiplies a by a b times), as well as tetration (in terms of our exp function).

2: a is stored in result of the tetration function.

3: Then we exponentiate a to result (as tetration does), and store that in result

4: Repeat step 3 b times.

Feel free to tell me I suck. (With huge credits to the commenters and Googology Wiki)

• Welcome to Code Golf! This looks like the start of a good answer, but there are a few obvious things that can be golfed (like the whitespace and variable names). You may want to check out Tips for Golfing in Python. Also, if you indent your code by four spaces here, it'll format it as code. Commented Feb 11, 2021 at 13:30
• Thanks for the advice! I'm going to format it now. Commented Feb 11, 2021 at 13:51
• I fixed a lot of issues, but I don't think this is optimal yet. Commented Feb 11, 2021 at 14:04
• Welcome @StackMeter, I suggest you take a look at this post: codegolf.stackexchange.com/questions/54/… A lot of good golfing tips Commented Feb 11, 2021 at 19:08
• Removed the spaces and dropped about 20 bytes; thanks! Commented Feb 11, 2021 at 19:16

# Wolfram Language (Mathematica), 29 bytes

(27 characters)

aNest[Nest[a#&,1,#]&,1,#]&


Try it online!

Input curried as f[x][y].

Without *: 35 bytes (33 characters)

aa+#&~n~0~n~1~n~1
n=Nest~Curry~3


Try it online!

Javascript: 116 chars

function t(i){y=i.split(' ');a=y[0];b=y[1];return+b&&p(a,t(a+' '+(b-1)))||1}function p(a,b){return+b&&a*p(a,b-1)||1}


t('4 3') Outputs:

1.3407807929942597e+154


### Python (111) (113) no *

r=lambda x,y:(x for _ in range(y));t=lambda x,y:reduce(lambda y,x:reduce(lambda x,y:sum(r(x,y)),r(x,y)),r(x,y),1)


6***3 - 36k digits))

Upd: Have to add initial value, to fit t(X,0)=1

• Impressive, how long did the 36k take? Commented Aug 17, 2012 at 17:56
• 9.375 seconds including print. Commented Aug 17, 2012 at 21:28

## Haskell: 88-5 chars without multiplication, 59 chars with multiplication

Without multiplication:

h x y=foldr(\x y->foldl(\x y->foldl(+)0(replicate x y))1(replicate y x))1(replicate y x)


There are probably ways that I could golf that down a little.

With multiplication:

h x y=foldr(\x y->foldl(*)1(replicate y x))1(replicate y x)


And finally, the ungolfed program:

mult x y = foldl (+) 0 (replicate x y)
expo x y = foldl (mult) 1 (replicate y x)
h x y = foldr (expo) 1 (replicate y x)


This is probably the simplest way to do this problem, which is defining multiplication as repeated addition, exponentiation as repeated multiplication, and tetration as repeated exponentiation.