34
3
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.
6The examples list
0!
but the challenge remarks say that the factorial count will be less than or equal to the base value. – Jonathan Allan – 2019-08-05T13:09:11.6801Wouldn't 3!!! be zero? n(n-3) = 3(3-3) = 0. – ouflak – 2019-08-05T13:31:25.663
2@ouflak If it works like 1!, not really. It's more like 1! = 1. 2!! = 2. 3!!! = 3. There's no calculation, because you are at the end of the recursiveness. No 0 in products or else every single factorial would drop down to 0 in the end. – V. Courtois – 2019-08-05T14:16:49.217
4
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. – Greg Martin – 2019-08-05T18:04:13.6731Thanks all! I reviewed the comments and have edited the post accordingly. It is indeed so that a factorial count higher than the base number should return the base number. It seems a bit unfair to change it now, but I think most answers comply with it anyway. – Jitse – 2019-08-06T07:16:08.010
1Tile should actually be "Repeated factorials!!!!". – val says Reinstate Monica – 2019-08-07T09:44:30.353
I'm surprised that nobody found a language yet where the solution is 0 or 1 Bytes. There are many that automatically print the output and surely there must be one that automatically parses the input as calculation or has one command to parse a string input as calculation and also supports repeated factorials. – Fabian Röling – 2019-08-07T14:32:30.090
3@FabianRöling: But that's not what this is. It's not
(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. – Peter Cordes – 2019-08-08T05:31:51.613