Skip to main content
6 of 9
solution to RAM bug, golfs another 11 bytes
Titus
  • 14.8k
  • 1
  • 24
  • 41

#PHP, 60+43+11 = 114 63 52 bytes

ignore the line breaks; they are for readability only.

function u($n,$m){return$m<2&$n<2?3**$n:u(u($n-1,$m),$m-1);}
function g($x){return u(3,$x>1?g($x-1):4);}
echo g(63);

It is possible to integrate the second case into the first one: for n=1, 3^n equals 3.
This will save a few bytes on - as far as I can see - all existing answers; saved two bytes on my

previous version, 62+43+11=116 bytes

function u($n,$m){return$m>1?$n>1?u(u($n-1,$m),$m-1):3:3**$n;}

PHP´s left associativity of the ternary requires parentheses ... or a specific order of tests.
This saved two bytes on the parenthesized expression.


There is an iterative approach, which may allow further golfing ...

g(m) is u(3,m), so let me see ...

m=1:
u(n,1) := 3^n

m=2: u(n,2)
    =u(u(n-1,2),1)      n=1: 3
    =3^u(n-1,2)         n=2: 3^u(1,2)=3^3
    =3^u(u(n-2,2),1)    n=3: 3^u(u(1,2),1)
    =3^3^u(n-2,2)           =3^3^u(1,2)=3^3^3
    =3^3^u(u(n-3,2),1)  n=4: 3^3^u(u(1,2),1)
    =3^3^3^u(n-3,2)         =3^3^3^u(1,2)=3^3^3^3
--> eval('$u=3'.str_repeat('^3',$n-1).';');

m=3: u(n,3) = u(u(n-1,3),2):
    $v=u($n-1,3);
    eval('$u=3'.str_repeat('^3',$v).';');

v=u(n-1,3) = u(u(n-2,3),2):
    $v=u(n-2,3);
    eval('$v=3'.str_repeat('**3',$v-1).',');

there it is: #PHP, 63 bytes

for(;$n<64;$n++)eval('$v=3'.str_repeat('**3',$v-1).';');echo$v;

to actually calculate it, use bc or gmp:

for(;$n<64;$n++)eval('$v='.str_repeat('bcpow(3,',$v-1).'1'.str_repeat(')',$v-1).';');echo$v;

oh wait ...
iteration will circumvent too deep nesting
bcpow() will circumvent integer overflow
but str_repeat() will kill the RAM somewhere


solution:

for($v=1;$n<64;$n++)for($i=$v;$i--;)$v=bcpow(3,$v);

or #PHP, 52 bytes

for($v=1;$n<64;$n++)for($i=$v;$i--;)$v=3**$v;echo$v;
Titus
  • 14.8k
  • 1
  • 24
  • 41