The factorial of a number, written as n! is defined as n! = n(n − 1)(n − 2)…(2)(1)
A generalization of this is the multifactorials where:
- n! = n(n − 1)(n − 2)…(2)(1)
- n!! = n(n − 2)(n − 4)…
- n!!! = n(n − 3)(n − 6)…
- n!!!! = n(n − 4)(n − 8)…
- n!!!!! = n(n − 5)(n − 10)…
- Where the products are for positive integers.
If we define the degree of the multifactorial as the difference in successive terms that are multiplied together for a multifactorial (The number of exclamation marks) then the task is to
- Write a function that given n and the degree, calculates the multifactorial.
- Use the function to generate and display here a table of the first 1..10 members of the first five degrees of multifactorial.
#include <algorithm> #include <iostream> #include <iterator> /*Generate multifactorials to 9 Nigel_Galloway November 14th., 2012. */ int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
- Output:
1 2 6 24 120 720 5040 40320 362880 3628800 1 2 3 8 15 48 105 384 945 3840 1 2 3 4 10 18 28 80 162 280 1 2 3 4 5 12 21 32 45 120 1 2 3 4 5 6 14 24 36 50 1 2 3 4 5 6 7 16 27 40 1 2 3 4 5 6 7 8 18 30 1 2 3 4 5 6 7 8 9 20 1 2 3 4 5 6 7 8 9 10
Content is available under GNU Free Documentation License 1.2.