4
\$\begingroup\$

Introduction

Factorials are one of the most frequently used examples to show how a programming language works. A factorial, denoted \$n!\$, is \$1⋅2⋅3⋅…⋅(n-2)⋅(n-1)⋅n\$.

There is also the superfactorial (there are other definitions of this, but I chose this one because it looks more like the factorial), denoted \$n$\$, which is equal to \$1!⋅2!⋅3!⋅…⋅(n-2)!⋅(n-1)!⋅n!\$.

From that you can create an infinity of (super)*factorials (means any number of times super followed by one factorial) (supersuperfactorial, supersupersuperfactorial, etc...) which all can be represented as a function \$‼(x,y)\$, which has two parameters, \$x\$ the number to (super)*factorialize, and y, the number of times plus one that you add the prefix super before factorial (So if \$y=1\$ it would be a factorial (\$x!=\space‼(x,1)\$), with \$y=2\$ a superfactorial, and with \$y=4\$ a supersupersuperfactorial).

\$!!\$ is defined as such:
\$‼(x,0)=x\$ (if \$y\$ is 0, return \$x\$)
\$‼(x,y)\space=\space‼(1,y-1)\space⋅\space‼(2,y-1)\space⋅\space‼(3,y-1)\space⋅…⋅\space‼(x-2,y-1)\space⋅\space‼(x-1,y-1)\$
The second definition would have looked cleaner with a pi product \$\prod\$, but it does not display properly. (image here)

In Python, \$!!\$ could be implemented this way:

from functools import reduce
def n_superfactorial(x, supers):
    if x == 0:
        return 1
    elif supers == 0:
        return x
    return reduce(lambda x,y:x*y, [n_superfactorial(i, supers-1) for i in range(1, x+1)])

Challenge

Create a function/program that calculates \$!!(x,y)\$.
Your code is not required to support floats or negative numbers.
\$x\$ will always be \$≥1\$ and \$y\$ will always be \$≥0\$.
It is not required to compute \$!!\$ via recursion.
Your program may exit with a recursion error given large numbers, but should at least theoretically be able to calculate in finite time any (super)*factorial.

Test cases

Format : [x, y] -> result
[1, 1]    -> 1
[2, 2]    -> 2
[3, 3]    -> 24
[3, 7]    -> 384
[4, 4]    -> 331776
[5, 3]    -> 238878720
[2232, 0] -> 2232
[3, 200]  -> 4820814132776970826625886277023487807566608981348378505904128

Note: if your language does not support integer types that are that large, you are not required to support the last test case.

Scoring

This is , so shortest answer in bytes wins.

\$\endgroup\$
12
  • 1
    \$\begingroup\$ I think (unfortunately) this is a dupe of the Torian challenge, since most of the algorithms there can be trivially ported here, and some even define !!(x,y) as a helper function. Most notably, Lynn's 4-byte Jelly answer works without modification here. \$\endgroup\$
    – Bubbler
    Commented Jul 30, 2021 at 13:21
  • \$\begingroup\$ @Bubbler I looked in the list of possible duplicates, but didn't see that (probably because it does not explicily mention the word factorial). \$\endgroup\$
    – astroide
    Commented Jul 30, 2021 at 13:42
  • 2
    \$\begingroup\$ The core of the challenges - implementing the function - are the same, and essentially all answers to the older one are portable to this. I've re-closed it because I believe this is a duplicate \$\endgroup\$ Commented Jul 31, 2021 at 11:34
  • 2
    \$\begingroup\$ FYI to those who suggested closing the older one: It works only if the older one has quality problems (more specifically, it was written before today's quality standard was established). Otherwise, the newer one is always the one to be closed. Generality of a task has never been a judging factor for closure. \$\endgroup\$
    – Bubbler
    Commented Jul 31, 2021 at 12:59
  • 1
    \$\begingroup\$ @user Yes, as long as you link to this question as the original. \$\endgroup\$
    – astroide
    Commented Aug 7, 2021 at 20:08

1 Answer 1

3
\$\begingroup\$

APL (Dyalog Unicode), 18 bytes

A naïve recursive implementation.

{×⍺:×/(⍺-1)∇¨⍳⍵⋄⍵}

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.