Not to be confused with Find the factorial!
Introduction
The factorial of an integer n
can be calculated by
$$n!=n\times(n-1)\times(n-2)\times(...)\times2\times1$$
This is relatively easy and nothing new. However, factorials can be extended to double factorials, such that $$n!!=n\times(n-2)\times(n-4)\times(...)\times4\times2$$ for even numbers, and $$n!!=n\times(n-2)\times(n-4)\times(...)\times3\times1$$ for odd numbers. But we're not limited to double factorials. For example $$n!!!=n\times(n-3)\times(n-6)\times(...)\times6\times3$$ or $$n!!!=n\times(n-3)\times(n-6)\times(...)\times5\times2$$ or $$n!!!=n\times(n-3)\times(n-6)\times(...)\times4\times1$$ depending on the starting value.
In summary: $${\displaystyle n!^{(k)}={\begin{cases}1&{\text{if }}n=0 \\n&{\text{if }}0<n\leq k\\n\cdot\left({(n-k)!}^{(k)}\right)&{\text{if }}n>k\end{cases}}}$$ where $${\displaystyle n!^{(k)}=n\underbrace{!\dots!}_{k}}$$ Or, in plain English: Subtract the factorial count from the base number repeatedly and multiply all resulting positive integers.
The Challenge
Write a function that will calculate any kind of repeated factorial for any non-negative integer.
Input
Either
- A string containing a non-negative base-ten integer, followed by 1 or more exclamation marks. E.g.
"6!"
or"9!!"
or"40!!!!!!!!!!!!!!!!!!!!"
.
or
- The same values represented by two integers: one non-negative base value and one positive value representing the factorial count. This can be done according to any format from the default I/O rules.
Output
The result of said calculation.
Challenge remarks
0!
equals1
by definition. Your code must account for this.- The factorial count is limited by $$ 0 < factorial~count \leq base~value $$outside this range, you are free to output whatever. Aside from
0!
, which is the only exception to this rule.
Examples
Input Output
3!!! 3
0! 1
6! 720
9!! 945
10!!!!!!!! 20
40!!!!!!!!!!!!!!!!!!!! 800
420!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 41697106428257280000000000000000
Try it with an ungolfed Python implementation: Try it online!
General remarks
- This is code-golf, so the answer using the fewest bytes in each language wins.
- Standard rules, I/O rules and loophole rules apply.
- Please include a Try it Online-link to demonstrate your code working.
- Please motivate your answer with an explanation of your code.
0!
but the challenge remarks say that the factorial count will be less than or equal to the base value. \$\endgroup\$3!!!!!!!
should not be undefined—it should just yield the answer3
. It's the same as1!!=1
(not undefined). Also your input specification says that there will always be at least one!
, so the first example3
doesn't fit the specification. \$\endgroup\$(3!)!
instead it's removing terms from a factorial. It's a misleading name; I came in assuming it was going to be applying the Factorial function repeatedly in a chain and had to read carefully to see what it actually was. Fortunately the question does explain it clearly. A better name might be stride factorial or step factorial or something. \$\endgroup\$