1

Just a question of curiosity, not serious.

In the JS language I know that a factorial operation can be defined in the following form.

function factorial(n) {
    if (n === 1) return 1;
    return factorial(n - 1) * n;
}

Then I defined a factorial of a factorial operation in the following form, and called it super_factorial_1. There is a similar description on Wikipedia.

function super_factorial_1(n) {
    if (n === 1) return factorial(1);
    return super_factorial_1(n - 1) * factorial(n);
}

Similarly, using the super_factorial_1 as an operator, the super_factorial_2 is defined here:

function super_factorial_2(n) {
    if (n === 1) return super_factorial_1(1);
    return super_factorial_2(n - 1) * super_factorial_1(n);
}

Now the question is how to define the super_factorial_n operation, and the super_factorial_n_n, and furthermore, the super_factorial_n..._n{n of n}.

I have used a crude method to define the above super_factorial_n operation, but I don't think this method is good enough.

function super_factorial_n(n, m) {
    const fns = Array(n + 1).fill(0);
    fns[0] = factorial;
    for (let i = 1; i <= n; i++) {
        fns[i] = function (m) {
            if (m === 1) return fns[i - 1](1);
            return fns[i](m - 1) * fns[i - 1](m);
        }
    }
    return fns[n](m);
}

Perhaps this is an optimisation direction for the process programming paradigm. :)

Lane Sun
  • 13
  • 4
  • all the theory around this stuff exists in fractal realities https://en.wikipedia.org/wiki/Fractal – Mister Jojo Dec 13 '21 at 13:37
  • "but I don't think this method is good enough." And the reason you do not think it is good enough? – epascarello Dec 13 '21 at 13:38
  • @epascarello This approach generates a large number of functions at runtime, but the purpose of generating these functions is not to implement a function factory, but simply to use them to derive values. So I thought there should be a way to simplify this process. This is similar to the dilemma we face when adding multi-threading support to a single-threaded language. – Lane Sun Dec 13 '21 at 13:46
  • You don't need to generate the sub-functions. They are already parameterized. Something like `function super_factorial(n, m) { if (n==0) return 1; if (m==1) return super_factorial(n-1, 1); else return super_factorial(n, m-1); * super_factorial(n-1, m) }`. (That's not quite right, but that's the idea. It was confusing following your discussion because `n` changed meaning partway through.) – Raymond Chen Dec 13 '21 at 14:32

1 Answers1

1

The pseudo code

// j is the level number
// i = j - 1
function super_factorial_j(n) {
    if (n === 1)
        return super_factorial_i(1);
    return super_factorial_j(n - 1) * super_factorial_i(n);
}

Parameterize the j and i

function super_factorial(j, n) {
    if (n === 1)
        return super_factorial(j - 1, 1);
    return super_factorial(j, n - 1) * super_factorial(j - 1, n);
}

Add the exit condition

function super_factorial(j, n) {
    if (j == 0) { // or j == 1 for one based level number
        if (n === 1)
            return 1;
        return super_factorial(0, n - 1) * n;
    }
    if (n === 1)
        return super_factorial(j - 1, 1);
    return super_factorial(j, n - 1) * super_factorial(j - 1, n);
}

Of course this is just one of the many ways to go. And it is hard to say if this is better then any other one. Recursive functions are generally stack memory consuming. But the value are likely grow very fast, it is not very practice to call it with big numbers anyway.

Todd Wong
  • 234
  • 1
  • 4