Multifactorial

From Rosetta Code
Revision as of 22:05, 13 November 2012 by rosettacode>Dkf (→‎Tcl: Added implementation)
Multifactorial is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The factorial of a number, written as is defined as

A generalization of this is the multifactorials where:

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

  1. Write a function that given n and the degree, calculates the multifactorial.
  2. Use the function to generate and display here a table of the first 1..10 members of the first five degrees of multifactorial.

Note: The wikipedia entry on multifactorials gives a different formula. This task uses the Wolfram mathworld definition.

Haskell

<lang haskell>mulfac k = 1:s where s = [1 .. k] ++ zipWith (*) s [k+1..]

-- for single n mulfac1 k n = product [n, n-k .. 1]

main = mapM_ (print . take 10 . tail . mulfac) [1..5]</lang>

Perl 6

<lang perl6>sub mfact($n, :$degree = 1) {

   [*] $n, $n - $degree ...^ * <= 0;

}

for 1 .. 5 -> $degree {

   say "$degree: ", map &mfact.assuming(:$degree), 1 .. 10;

}</lang>

Output:
1: 1 2 6 24 120 720 5040 40320 362880 3628800
2: 1 2 3 8 15 48 105 384 945 3840
3: 1 2 3 4 10 18 28 80 162 280
4: 1 2 3 4 5 12 21 32 45 120
5: 1 2 3 4 5 6 14 24 36 50

Python

Python: Iterative

<lang python>>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m))

>>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)]))

1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840]
3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280]
4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120]
5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50]
6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40]
7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30]
8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> </lang>

Python: Recursive

<lang python>>>> def mfac2(n, m): return n if n <= (m + 1) else n * mfac2(n - m, m)

>>> for m in range(1, 6): print("%2i: %r" % (m, [mfac2(n, m) for n in range(1, 11)]))

1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840]
3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280]
4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120]
5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50]

>>> </lang>

Tcl

Works with: Tcl version 8.6

<lang tcl>package require Tcl 8.6

proc mfact {n m} {

   set mm [expr {-$m}]
   for {set r $n} {[incr n $mm] > 1} {set r [expr {$r * $n}]} {}
   return $r

}

foreach n {1 2 3 4 5 6 7 8 9 10} {

   puts $n:[join [lmap m {1 2 3 4 5 6 7 8 9 10} {mfact $m $n}] ,]

}</lang>

Output:
1:1,2,6,24,120,720,5040,40320,362880,3628800
2:1,2,3,8,15,48,105,384,945,3840
3:1,2,3,4,10,18,28,80,162,280
4:1,2,3,4,5,12,21,32,45,120
5:1,2,3,4,5,6,14,24,36,50
6:1,2,3,4,5,6,7,16,27,40
7:1,2,3,4,5,6,7,8,18,30
8:1,2,3,4,5,6,7,8,9,20
9:1,2,3,4,5,6,7,8,9,10
10:1,2,3,4,5,6,7,8,9,10