Tetration – Solution

Tetration is a bizarre mathematical concept. I’ve watched quite a few YouTube videos where math geeks explain the details. They venture into the terrifying territory of humongous numbers that would consume the known universe. Your programming challenge for this month’s Exercise is far less massive.

My own trials proved that I needed a tetration function to perform the calculation in C. Further, and more frightening, this function needed to be recursive. After all, to work the exponential tower the nested exponents must be calculated from the top down.

My solution has the user input the base and the exponent. A test is done to confirm that the value input is two or greater. As I wrote in the Exercise post, I don’t know whether or how tetration deals with values less than two.

Here is my solution:

2023_12-Exercise.c

#include <stdio.h>
#include <math.h>

double tetrate(double a, int b)
{
    double c = 0.0;

    while(b--)
        return(c = pow(a,tetrate(a,b)));
    return(c);
}

int main()
{
    int b,e;
    double r;

    printf("Base: ");
    scanf("%d",&b);
    printf("Exponent: ");
    scanf("%d",&e);

    if( e<2 )
    {
        printf("Please specify a value larger than 2\n");
    }
    else
    {
        r = tetrate((double)b,e+1);
        printf("%d^^%d = %.f\n",b,e,r);
    }

    return(0);
}

The tetrate() function accepts a double value a, the base, and an integer b, the exponent. The scanf() functions read integer values. Variable b is typecast to double when it’s passed to the tetrate() function.

The function uses a while loop that repeats as long as the value of b is greater than zero. The sole looping statement is a return, which contains the pow() function. It calculates the exponent of a, using the return value from the tetrate() function of a to b. The function recurses until b is exhausted.

Variable c is used to capture the pow() function’s return value, but also to avoid a warning about the function not returning a value. Oddly enough, the program works without variable c, but I added it to ensure that everything was okee-doke with the compiler.

Remember that on some systems you must link in the math library to build this program. Use the -lm switch at the command line or perform whatever magic is required in the IDE to add the math library.

Here is a sample run:

Base: 4
Exponent: 3
4^^3 = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096

And:

Base: 5
Exponent: 3
5^^3 = inf

Without linking in a huge number library, the values you can plug into this program are rather limited — well, limited in the full scope of the terrifying values tetration generates. I haven’t tested the code on anything other than values 2, 3, 4, and 5. Tune into some of those tetration YouTube videos to see how insanely vast the numbers get.

Being a math novice, I hope my solution is accurate! Regardless, I hope that your solution met with success.

Leave a Reply