Googology Wiki
Googology Wiki
Googology Wiki

Due to the relationship \(f_2(n) = 2^n n\), the fast-growing hierarchy (FGH) is often approximated using 2-based arrow notation. In this article, we adopt 10-based arrow notation for approximating FGH, as it is more commonly used and facilitates easier comparison with other googological numbers.

In conclusion, when m > 2 and n is sufficiently large, the approximation \(f_m(n) \approx 10 \uparrow^{m-1} n\) is reasonable. For smaller values of n, the specific values listed in this article can be used for a more accurate assessment. As continuous arrow notation is employed, it allows for a more precise evaluation of the size of numbers compared to most googological functions defined with integer values. The calculation program used to generate the tables in this article is provided at the bottom of the page. Therefore, for values of m and n not listed in this article, the program can be used.

f_3(n)[]

\(f_0(n) = n+1\), \(f_1(n) = 2n\) and \(f_2(n) = 2^n n\) can be easily calculated, so we start from \(f_3(n)\). \(f_3(n)\) was calculated and expressed by tetration of base 10 as follows.

Linear approximation of tetration as shown in tetration is used.

\begin{equation} {}^{y}x = \begin{cases} \log_x(^{y+1}x) & y \le -1 \\ 1 + y & -1 < y \le 0 \\ x^{\left(^{y-1}x\right)} & 0 < y \end{cases} \end{equation}

  • f3(1) = 2
  • f3(2) = 2048
  • f3(3) ≈ 6.895079 x 10 ^ 121210694
  • f3(4) ≈ 10^^4.118207440274801
  • f3(5) ≈ 10^^5.229846529550299
  • f3(6) ≈ 10^^6.31610106672771
  • f3(7) ≈ 10^^7.3864658603261475
  • f3(8) ≈ 10^^8.44589995808178
  • f3(9) ≈ 10^^9.497358873079769
  • f3(10) ≈ 10^^10.542750880844022
  • f3(11) ≈ 10^^11.583377636905475
  • f3(12) ≈ 10^^12.620164522737596
  • f3(13) ≈ 10^^13.653791583104528
  • f3(14) ≈ 10^^14.684772510968287
  • f3(15) ≈ 10^^15.713504499210245
  • f3(16) ≈ 10^^16.74030087722953
  • f3(17) ≈ 10^^17.765413146733252
  • f3(18) ≈ 10^^18.789046263312397
  • f3(19) ≈ 10^^19.81136948696456
  • f3(20) ≈ 10^^20.832524249998386
  • f3(21) ≈ 10^^21.852629970693947
  • f3(22) ≈ 10^^22.87178842255439
  • f3(23) ≈ 10^^23.89008706871018
  • f3(24) ≈ 10^^24.90760164213431
  • f3(25) ≈ 10^^25.92439816757554
  • f3(26) ≈ 10^^26.940534564294815
  • f3(27) ≈ 10^^27.956061929901814
  • f3(28) ≈ 10^^28.971025578665092
  • f3(29) ≈ 10^^29.985465888690747
  • f3(30) ≈ 10^^30.99941899879284
  • f3(31) ≈ 10^^32.00557402556907
  • f3(32) ≈ 10^^33.01114327507603
  • f3(33) ≈ 10^^34.01647524921984
  • f3(34) ≈ 10^^35.021587695941605
  • f3(35) ≈ 10^^36.02649650582133
  • f3(36) ≈ 10^^37.03121595826215
  • f3(37) ≈ 10^^38.035758928606334
  • f3(38) ≈ 10^^39.04013706334312
  • f3(39) ≈ 10^^40.04436092909055
  • f3(40) ≈ 10^^41.048440139892875
  • f3(41) ≈ 10^^42.05238346648771
  • f3(42) ≈ 10^^43.05619893050205
  • f3(43) ≈ 10^^44.05989388598748
  • f3(44) ≈ 10^^45.063475090269094
  • f3(45) ≈ 10^^46.06694876573435
  • f3(46) ≈ 10^^47.070320653907814
  • f3(47) ≈ 10^^48.073596062931394
  • f3(48) ≈ 10^^49.07677990938551
  • f3(49) ≈ 10^^50.07987675523597
  • f3(50) ≈ 10^^51.08289084056827
  • f3(51) ≈ 10^^52.08582611266858
  • f3(52) ≈ 10^^53.088686251926816
  • f3(53) ≈ 10^^54.09147469496661
  • f3(54) ≈ 10^^55.094194655348474
  • f3(55) ≈ 10^^56.09684914214347
  • f3(56) ≈ 10^^57.09944097663282
  • f3(57) ≈ 10^^58.10197280735456
  • f3(58) ≈ 10^^59.10444712368823
  • f3(59) ≈ 10^^60.106866268143705
  • f3(60) ≈ 10^^61.109232447498655
  • f3(61) ≈ 10^^62.111547742910815
  • f3(62) ≈ 10^^63.1138141191154
  • f3(63) ≈ 10^^64.11603343280473
  • f3(64) ≈ 10^^65.1182074402748
  • f3(65) ≈ 10^^66.12033780441442
  • f3(66) ≈ 10^^67.12242610110262
  • f3(67) ≈ 10^^68.12447382507335
  • f3(68) ≈ 10^^69.12648239529933
  • f3(69) ≈ 10^^70.12845315994124
  • f3(70) ≈ 10^^71.13038740090354
  • f3(71) ≈ 10^^72.13228633803342
  • f3(72) ≈ 10^^73.13415113299571
  • f3(73) ≈ 10^^74.13598289285326
  • f3(74) ≈ 10^^75.13778267337878
  • f3(75) ≈ 10^^76.13955148212206
  • f3(76) ≈ 10^^77.14129028125353
  • f3(77) ≈ 10^^78.14299999020359
  • f3(78) ≈ 10^^79.14468148811466
  • f3(79) ≈ 10^^80.14633561612172
  • f3(80) ≈ 10^^81.14796317947548
  • f3(81) ≈ 10^^82.14956494952081
  • f3(82) ≈ 10^^83.15114166554204
  • f3(83) ≈ 10^^84.15269403648583
  • f3(84) ≈ 10^^85.15422274257094
  • f3(85) ≈ 10^^86.15572843679374
  • f3(86) ≈ 10^^87.15721174633748
  • f3(87) ≈ 10^^88.15867327389236
  • f3(88) ≈ 10^^89.16011359889325
  • f3(89) ≈ 10^^90.1615332786809
  • f3(90) ≈ 10^^91.1629328495925
  • f3(91) ≈ 10^^92.1643128279862
  • f3(92) ≈ 10^^93.16567371120485
  • f3(93) ≈ 10^^94.16701597848271
  • f3(94) ≈ 10^^95.16834009179941
  • f3(95) ≈ 10^^96.16964649668462
  • f3(96) ≈ 10^^97.17093562297697
  • f3(97) ≈ 10^^98.17220788553993
  • f3(98) ≈ 10^^99.173463684938
  • f3(99) ≈ 10^^100.17470340807532
  • f3(100) ≈ 10^^101.17592742879957
  • f3(1000) ≈ 10^^1001.39483169892
  • f3(10000) ≈ 10^^10001.541468327436
  • f3(100000) ≈ 10^^100001.65114948843
  • f3(1000000) ≈ 10^^1000001.7386709944
  • f3(10000000) ≈ 10^^10000001.811481884
  • f3(100000000) ≈ 10^^100000001.87382089
  • f3(1000000000) ≈ 10^^1000000001.9283247
  • f3(10000000000) ≈ 10^^10000000001.976744
  • f3(100000000000) ≈ 10^^100000000002.00873
  • f3(1000000000000) ≈ 10^^1000000000002.0253
  • f3(10000000000000) ≈ 10^^10000000000002.04
  • f3(100000000000000) ≈ 10^^100000000000002.05

From this result, the following approximation is justified when n is large enough.

\[f_3(n) \approx 10 \uparrow\uparrow n\]

f_4(n)[]

Based on this approximation, \(f_4(n)\) is approximated as follows. Continuous arrow notation, defined as

\begin{equation} a \uparrow^n x = \begin{cases} \underbrace{ss...s}_{n-2 s's}log_a(a \uparrow^n (x+1)) & x \le -1 \\ 1 + x & -1 < x \le 0 \\ a \uparrow^{n-1} (a \uparrow^n (x-1)) & 0 < x \end{cases} \end{equation}

is used.

  • \(f_4(1) = 2\)
  • \(f_4(2) = f_3(f_2(f_2(2)) = f_3(2048) \approx 10\uparrow\uparrow 2049.445899958 \approx 10 \uparrow\uparrow\uparrow 2.1818\)
  • \(f_4(3) = f_3^3(3) \approx f_3^2(10\uparrow\uparrow 2.9076) \approx 10\uparrow\uparrow 10\uparrow\uparrow 10\uparrow\uparrow 2.9076 \approx 10\uparrow\uparrow\uparrow 3.4635\)
  • \(f_4(4) \approx 10\uparrow\uparrow 10\uparrow\uparrow 10\uparrow\uparrow 10\uparrow\uparrow 4.1182 \approx 10\uparrow\uparrow\uparrow 4.6147\)
  • \(f_4(5) \approx 10 \uparrow\uparrow\uparrow 5.718488944632594\)
  • \(f_4(6) \approx 10 \uparrow\uparrow\uparrow 6.800449070730014\)
  • \(f_4(7) \approx 10 \uparrow\uparrow\uparrow 7.868436694892096\)
  • \(f_4(8) \approx 10 \uparrow\uparrow\uparrow 8.926645932889226\)
  • \(f_4(9) \approx 10 \uparrow\uparrow\uparrow 9.977602848833962\)
  • \(f_4(10) \approx 10 \uparrow\uparrow\uparrow 11.00425948512775\)
  • \(f_4(11) \approx 10 \uparrow\uparrow\uparrow 12.011517311087218\)
  • \(f_4(12) \approx 10 \uparrow\uparrow\uparrow 13.017789757490174\)
  • \(f_4(13) \approx 10 \uparrow\uparrow\uparrow 14.023290643457484\)
  • \(f_4(14) \approx 10 \uparrow\uparrow\uparrow 15.02817314625803\)
  • \(f_4(15) \approx 10 \uparrow\uparrow\uparrow 16.03255039657724\)
  • \(f_4(16) \approx 10 \uparrow\uparrow\uparrow 17.0365080945581\)
  • \(f_4(17) \approx 10 \uparrow\uparrow\uparrow 18.040112567545812\)
  • \(f_4(18) \approx 10 \uparrow\uparrow\uparrow 19.04341610052022\)
  • \(f_4(19) \approx 10 \uparrow\uparrow\uparrow 20.046460569667534\)
  • \(f_4(20) \approx 10 \uparrow\uparrow\uparrow 21.049279984244812\)
  • \(f_4(100) \approx 10 \uparrow\uparrow\uparrow 101.11465471078382\)
  • \(f_4(1000) \approx 10 \uparrow\uparrow\uparrow 1001.1694419092759\)
  • \(f_4(10000) \approx 10 \uparrow\uparrow\uparrow 10001.204680745006\)
  • \(f_4(100000) \approx 10 \uparrow\uparrow\uparrow 100001.23018587059\)
  • \(f_4(1000000) \approx 10 \uparrow\uparrow\uparrow 1000001.2499687128\)
  • \(f_4(10000000) \approx 10 \uparrow\uparrow\uparrow 10000001.266019449\)
  • \(f_4(100000000) \approx 10 \uparrow\uparrow\uparrow 100000001.27945933\)
  • \(f_4(1000000000) \approx 10 \uparrow\uparrow\uparrow 1000000001.2909784\)
  • \(f_4(10000000000) \approx 10 \uparrow\uparrow\uparrow 10000000001.30103\)
  • \(f_4(10\uparrow\uparrow 10) \approx f_3^{10\uparrow\uparrow 10-1}(10\uparrow\uparrow 10\uparrow\uparrow 10) \approx 10\uparrow\uparrow\uparrow (10\uparrow\uparrow 10+2)\)

From this result, the following approximation is justified when n is large.

\[f_4(n) \approx 10 \uparrow\uparrow\uparrow n\]

f_5(n)[]

Based on this approximation, \(f_5(n)\) is approximated as follows.

  • \(f_5(1) = 2\)
  • \(f_5(2) \approx f_4(10\uparrow\uparrow 2049) \approx 10\uparrow\uparrow\uparrow 10\uparrow\uparrow 2049\)
  • \(f_5(3) \approx f_4^2(10\uparrow\uparrow\uparrow 3.4635) \approx 10\uparrow\uparrow\uparrow 10\uparrow\uparrow\uparrow 10\uparrow\uparrow\uparrow 3.4635 \approx 10\uparrow^4 3.5395\)
  • \(f_5(4) \approx 10 \uparrow^4 4.6641442464390055\)
  • \(f_5(5) \approx 10 \uparrow^4 5.757281285837918\)
  • \(f_5(6) \approx 10 \uparrow^4 6.832537592485718\)
  • \(f_5(7) \approx 10 \uparrow^4 7.8958884550753305\)
  • \(f_5(8) \approx 10 \uparrow^4 8.950688309228667\)
  • \(f_5(9) \approx 10 \uparrow^4 9.99902621317376\)
  • \(f_5(10) \approx 10 \uparrow^4 11.00329385599431\)
  • \(f_5(11) \approx 10 \uparrow^4 12.006128087880635\)
  • \(f_5(12) \approx 10 \uparrow^4 13.008594174812226\)
  • \(f_5(13) \approx 10 \uparrow^4 14.010767785855151\)
  • \(f_5(14) \approx 10 \uparrow^4 15.012704291804335\)
  • \(f_5(15) \approx 10 \uparrow^4 16.01444529447999\)
  • \(f_5(16) \approx 10 \uparrow^4 17.01602275403633\)
  • \(f_5(17) \approx 10 \uparrow^4 18.017461695031816\)
  • \(f_5(18) \approx 10 \uparrow^4 19.01878203655974\)
  • \(f_5(19) \approx 10 \uparrow^4 20.01999986345067\)
  • \(f_5(20) \approx 10 \uparrow^4 21.02112832998889\)
  • \(f_5(100) \approx 10 \uparrow^4 101.04713294995302\)
  • \(f_5(1000) \approx 10 \uparrow^4 1001.0679771087036\)
  • \(f_5(10000) \approx 10 \uparrow^4 10001.080871813687\)
  • \(f_5(100000) \approx 10 \uparrow^4 100001.08997072026\)
  • \(f_5(1000000) \approx 10 \uparrow^4 1000001.0968991413\)
  • \(f_5(10000000) \approx 10 \uparrow^4 10000001.102440378\)
  • \(f_5(100000000) \approx 10 \uparrow^4 100000001.10702649\)
  • \(f_5(1000000000) \approx 10 \uparrow^4 1000000001.110919\)
  • \(f_5(10000000000) \approx 10 \uparrow^4 10000000001.114286\)
  • \(f_5(10\uparrow\uparrow\uparrow 10) \approx 10\uparrow^4 (10\uparrow\uparrow\uparrow 10+2)\)

From this result, the following approximation is justified when n is large.

\[f_5(n) \approx 10 \uparrow^4 n\]

f_m(n)[]

Based on the results so far, it would be safe to approximate as follows when m>2 and n is large.

\[f_m(n) \approx 10 \uparrow^{m-1} n\]

For a relatively small value of n, it is better to directly calculate by using the approximations shown in this page or by using the following program.

Calculation program[]

Hypercalc can run BASIC program. Run Hypercalc and show help basic for instruction on how to use it. As an example, a program for calculating \(f_3(n)\) for \(10<n<10^{10}\) is as follows.

10 ' Calculate f_3(n) in FGH for n>10
20 let n = 10;
25 let m = n;
30 for i=1 to 10;
40 n = 2^n*n;
50 next i
60 for i=1 to 10;
70 n = log(n);
80 next i;
90 print "f_3(";m;") = E";n;"#";m
100 n = log(n);
110 print "if 0 < ";n;" < 1"
120 print "f_3(";m;") = 10^^";m+n
130 n = log(n);
140 print "if 0 < ";n;" < 1"
150 n=m+n+1;
160 print "f_3(";m;") = 10^^";n
170 end

This code uses the power-tower paradox.

Hypercalc does not allow the use of "if" statement. Therefore I created a Python program to invoke hypercalc as follows. Just set a path to hypercalc in the variable HYPERCALC and run this with Python 3. For calculating \(f_m(n)\), input m and n separating with a space. For displaying a table of \(f_m(n)\), input m. In the comment of the test function, calculation process is shown for verifying the accuracy of the program.

#!/usr/bin/env python3
import math
HYPERCALC = '(PATH)/hypercalc'
HELPURL = 'https://googology.fandom.com/wiki/User_blog:Kyodaisuu/Approximation_of_FGH_with_10-based_arrow_notation'


def main():
    import sys
    import webbrowser
    test()
    print('For calculating f_m(n), input m and n separating with a space: 3 3')
    print('For displaying a table of f_m(n), input m: 4')
    print('For showing help website, input nothing.')
    line = input('Input: ')
    if len(line) == 0:
        webbrowser.open(HELPURL)
        sys.exit()
    lineInt = list(map(int, line.split(' ')))
    if len(lineInt) == 1:
        m = lineInt[0]
        assert m > -1
        table(m)
        sys.exit()
    m, n = lineInt
    assert m > -1
    assert n > 0
    if m < 10:
        mm = str(m)
    else:
        mm = '{' + str(m) + '}'
    print('f_{0}({1}) = {2}'.format(m, n, fgh_chain(m, n)))
    if m > 3:
        print('\(f_{0}({1}) = {2}\)'.format(mm, n, fgh(m, n)))


def test():
    # hc_result(equation) performs one-line calculation of Hypercalc
    assert hc_result('2^3') == '8'
    assert hc_result('4^4^4') == '1.3407807929941 x 10 ^ 154'
    # fgh(m, n) calculates f_m(n)
    assert fgh(2, 20) == '20971520'
    # because f_2(20) = 20*2^20 = 2097152
    assert fgh(3, 2) == '2048'
    # because f_3(2) = f_2(f_2(2)) = f_2(8) = 2048
    assert fgh(3, 5) == '10^^5.229846529550299'
    # f_3(5) = f_2^5(5)
    #        = f_2^4(5*2^5) = f_2^4(160)
    #        = f_2^3(160*2^160) = 10^50.3689192888929
    #        = f_2^2(10^10^49.8475290612385)
    #        = f_2(10^10^10^49.8475290612385)
    #        = 10^10^10^10^49.8475290612385
    #        = 10^10^10^10^10^1.6976436352321
    #        = 10^10^10^10^10^10^0.2298465295502
    #        = 10^^5.2298465295502
    assert fgh(3, 100) == '10^^101.17592742879957'
    # f_3(100) = f_2^100(100)
    #          = f_2^99(10^32.1029995663981)
    #          = f_2^98(10^10^31.5816093387438)
    #          = f_2^97(10^10^10^31.581609338743)
    #          = f_2^97(10^10^10^10^1.4994342570568)
    #          = f_2^97(10^10^10^10^10^0.1759274287995)
    #          = f_2^97(10^^4.175927428799)
    #          = f_2^96(10^^5.175927428799)
    #          = 10^^101.175927428799
    assert fgh(3, 100000000000) == '10^^100000000002.00873'
    # fgh_chain returns result with chained arrow notation for m>3
    # fgh uses fgh_chain and convert it to arrow notation for m>3
    assert fgh_chain(4, 3) == '10→3.4635349054779008→3'
    # f_4(3) = f_3(f_3(f_3(3)))
    #        = f_3(f_3(6.895079 x 10 ^ 121210694))
    #        = f_3(f_3(10^121210694.8385386795705))
    #        = f_3(f_3(10^10^8.083540940824))
    #        = f_3(f_3(10^^2.9076016422346))
    #        = f_3(10^^10^^2.907601642234)
    #        = 10^^10^^10^^2.907601642234
    #        = 10^^10^^10^^10^0.4635349054778
    #        = 10^^10^^10^^10^^0.4635349054778
    #        = 10^^^3.4635349054778
    #        = 10→3.4635349054778892→3
    assert fgh_chain(4, 10) == '10→11.00425948512775→3'
    assert fgh_chain(4, 100) == '10→101.11465471078382→3'
    # f_4(100) = f_3^99(f_3(100))
    #          = f_3^99(10^^101.17592742879)
    #          = f_3^99(10^^10^10^0.3021310973056)
    #          = f_3^99(10^^10^^1.3021310973056)
    #          = ((10^^)99)(10^^10^^1.3021310973056)
    #          = ((10^^)99)(10^^10^^10^^0.1146547107838)
    #          = 10^^^101.1146547107838
    assert fgh_chain(5, 5) == '10→5.757281285837918→4'
    assert fgh_chain(5, 100) == '10→101.04713294995302→4'
    # f_5(100) = ((10^^^)^99)f_4(100)
    #          = ((10^^^)^99)10^^^101.1146547107838
    #          = ((10^^^)^99)10^^^10^2.0048141031557
    #          = ((10^^^)^99)10^^^10^10^0.3021310973056
    #          = ((10^^^)^99)10^^^10^^1.3021310973056
    #          = ((10^^^)^99)10^^^10^^10^^0.1146357032131
    #          = ((10^^^)^99)10^^^10^^^1.1146357032131
    #          = ((10^^^)^99)10^^^10^^^10^^^0.047132949953
    #          = 10^^^^101.0471403557921
    assert fgh_chain(6, 100) == '10→101.01999843801991→5'
    # f_6(100) = f_5^99(f_5(100))
    #          = f_5^99(10^^^^101.0471403557921)
    #          = f_5^99(10^^^^10^10^0.3020112663363)
    #          = f_5^99(10^^^^10^^1.3020112663363
    #          = f_5^99(10^^^^10^^^1.1146147422095)
    #          = f_5^99(10^^^^10^^^^1.0471247828597)
    #          = f_5^99(10^^^^10^^^^10^^^^0.0199984383916)
    #          = ((10^^^^)^99)(10^^^^10^^^^0.0199984383916)
    #          = 10^^^^^101.0199984383916
    assert fgh_chain(10, 100) == '10→101.0006994567718→9'
    assert fgh_chain(20, 100) == '10→101.00000016685297→19'
    assert fgh_chain(30, 100) == '10→101.00000000003983→29'


def fgh(m, n):
    f = fgh_chain(m, n)
    if '→' in f:
        a, b, c = f.split('→')
        if int(c) == 3:
            arrow = '\\uparrow\\uparrow\\uparrow'
        elif int(c) < 10:
            arrow = '\\uparrow^{0}'.format(c)
        else:
            arrow = '\\uparrow^{{{0}}}'.format(c)
        f = '{0} {1} {2}'.format(a, arrow, b)
    return f


def fgh_chain(m, n):
    assert m == int(m)
    assert m > -1
    if m == 0:
        return str(n+1)
    if m == 1:
        return str(2*n)
    if m == 2:
        if n < 94:
            return str(n * 2**n)
        result = n * math.log10(2) + math.log10(n)
        return '10 ^ {0}'.format(result)
    if m == 3:
        return f3(n)
    assert n == int(n)
    assert n > 0
    if n == 1:
        return '2'
    prev = fgh_chain(m-1, n)
    if m == 4:
        if n == 2:
            f = float(fgh(3, int(fgh(3, 2))).split('^^')[1])
            current = math.log10(math.log10(math.log10(f))+1) + 2
            result = '10→{0}→3'.format(current)
            return result
        elif n == 3:
            # Equation for log_10(f_3(3)) = 121210694.83853936
            logf33 = (3 * 2**27 + 27) * math.log10(2) + math.log10(3)
            prev = str(math.log10(math.log10(logf33)) + 2)
        else:
            prev = prev.split('^^')[1]
    else:
        prev = prev.split('→')[1]
    current = math.log10(float(prev))
    if current > 1:
        for i in range(m-2):
            current = math.log10(current) + 1
    current += n
    result = '10→{0}→{1}'.format(current, m-1)
    return result


def table(m):
    assert m == int(m)
    if m < 4:
        simple_table(m)
    else:
        mathjax_table(m)


def mathjax_table(m):
    table = list(range(1, 21))
    if m < 10:
        mm = str(m)
    else:
        mm = '{' + str(m) + '}'
    for i in range(2, 11):
        table.append(10**i)
    for n in table:
        result = fgh(m, n)
        if 'uparrow' in result:
            eq = '\\approx'
        else:
            eq = '='
        print('* \(f_{0}({1}) {2} {3}\)'.format(mm, n, eq, result))


def simple_table(m):
    table = list(range(1, 100))
    for i in range(2, 15):
        table.append(10**i)
    for n in table:
        result = fgh(m, n)
        if '^' in result:
            eq = '≈'
        else:
            eq = '='
        print('* f<sub>{0}</sub>({1}) {2} {3}'.format(m, n, eq, result))


def f3(n):
    assert n == int(n)
    assert n > 0
    if n < 10:
        basic = '''10 let n = {0};
20 let m = n;
30 for i=1 to n;
40 n = 2^n*n;
50 next i
60 print n
70 end
run'''.format(n)
        result = plain(hc_basic(basic))
    else:
        basic = '''10 let n = {0};
20 let m = n;
30 for i=1 to 10;
40 n = 2^n*n;
50 next i
60 print n
70 end
run'''.format(n)
        tet = plain(hc_basic(basic)).split('^^')[1]
        tet = float(tet) + n - 10
        result = '10^^' + str(tet)
    return result


def plain(exp):
    if '(' not in exp:
        return exp
    if 'PT' in exp:
        pt, exp = exp.split('PT')
        pt = int(pt)
    else:
        pt = 0
    while exp[:5] == '10 ^ ':
        pt += 1
        exp = exp[7:]
    exp = exp.replace('(', '').replace(')', '').replace(']', '').strip()
    if pt > 1:
        if '^' in exp:
            base = float(exp.split('x')[0])
            index = float(exp.split('^')[1]) + math.log10(base)
        else:
            index = math.log10(int(exp))
        while index > 1:
            index = math.log10(index)
            pt += 1
    tetration = pt + index
    return '10^^' + str(tetration)


def hc_basic(basic):
    result = hypercalc(basic.splitlines())
    for line in result.splitlines():
        if '=' in line and line[0] == 'C':
            rr = line.split(' = ')[1]
            if len(rr) > 0:
                r = rr
    return r


def hc_result(command):
    output = hypercalc([command,])
    result = ''
    for line in output.splitlines():
        if '=' in line and line[0] == 'R':
            result = line.split(' = ')[1]
    return result


def hypercalc(command):
    import os
    import subprocess
    shell = "cat << EOF | " + os.path.expanduser(HYPERCALC) + "\n"
    for c in command:
        shell += c + '\n'
    shell += "quit\nEOF"
    output = subprocess.run(shell, capture_output=True,
                            shell=True, text=True).stdout
    return output


if __name__ == "__main__":
    main()