Multifactorial: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
m (added whitespace to the task's preamble, used a larger font to show the formulas.)
Line 1: Line 1:
{{task}}
{{task}}
The factorial of a number, written as <math>n!</math>, is defined as <math>n! = n(n-1)(n-2)...(2)(1)</math>.


The factorial of a number &nbsp; (written as &nbsp; <big><big><math> n! </math></big></big>), &nbsp; &nbsp; is defined as: <big><big>
[http://mathworld.wolfram.com/Multifactorial.html Multifactorials] generalize factorials as follows:
: <math>n! = n(n-1)(n-2)...(2)(1)</math>
::: <math> n! = n(n-1)(n-2)...(2)(1) </math>
</big></big>
: <math>n!! = n(n-2)(n-4)...</math>
: <math>n!! ! = n(n-3)(n-6)...</math>
: <math>n!! !! = n(n-4)(n-8)...</math>
: <math>n!! !! ! = n(n-5)(n-10)...</math>



[http://mathworld.wolfram.com/Multifactorial.html Multifactorials] generalize factorials as follows: <big><big>
::: <math> n! = n(n-1)(n-2)...(2)(1) </math>
::: <math> n!! = n(n-2)(n-4)... </math>
::: <math> n!! ! = n(n-3)(n-6)... </math>
::: <math> n!! !! = n(n-4)(n-8)... </math>
::: <math> n!! !! ! = n(n-5)(n-10)... </math>
</big></big>

<br>
In all cases, the terms in the products are positive integers.
In all cases, the terms in the products are 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 twofold:
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 twofold:
# Write a function that given n and the degree, calculates the multifactorial.
# Write a function that given &nbsp; '''n''' &nbsp; and the degree, calculates the multifactorial.
# Use the function to generate and display here a table of the first ten members (1 to 10) of the first five degrees of multifactorial.
# Use the function to generate and display here a table of the first ten members (1 to 10) of the first five degrees of multifactorial.


<br>
<small>'''Note:''' The [[wp:Factorial#Multifactorials|wikipedia entry on multifactorials]] gives a different formula. This task uses the [http://mathworld.wolfram.com/Multifactorial.html Wolfram mathworld definition].</small>
<small>'''Note:''' The [[wp:Factorial#Multifactorials|wikipedia entry on multifactorials]] gives a different formula. This task uses the [http://mathworld.wolfram.com/Multifactorial.html Wolfram mathworld definition].</small>
<br><br>


=={{header|Ada}}==
=={{header|Ada}}==

Revision as of 08:59, 9 May 2016

Task
Multifactorial
You are encouraged to solve this task according to the task description, using any language you may know.

The factorial of a number   (written as   ),     is defined as:


Multifactorials generalize factorials as follows:


In all cases, the terms in the products are 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 twofold:

  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 ten members (1 to 10) of the first five degrees of multifactorial.


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

Ada

<lang Ada>with Ada.Text_IO; use Ada.Text_IO; procedure Mfact is

  function MultiFact (num : Natural; deg : Positive) return Natural is
     Result, N : Integer := num;
  begin
     if N = 0 then return 1; end if;
     loop
        N := N - deg; exit when N <= 0; Result := Result * N;
     end loop; return Result;
  end MultiFact;

begin

  for deg in 1..5 loop
     Put("Degree"& Integer'Image(deg) &":");
     for num in 1..10 loop Put(Integer'Image(MultiFact(num,deg))); end loop;
     New_line;
  end loop;

end Mfact;</lang>

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

ALGOL 68

Translation of C. <lang Algol68>BEGIN

  INT highest degree = 5;
  INT largest number = 10;

CO Recursive implementation of multifactorial function CO

  PROC multi fact = (INT n, deg) INT :
  (n <= deg | n | n * multi fact(n - deg, deg));

CO Iterative implementation of multifactorial function CO

  PROC multi fact i = (INT n, deg) INT :
  BEGIN
     INT result := n, nn := n;
     WHILE (nn >= deg + 1) DO

result TIMESAB nn - deg; nn MINUSAB deg

     OD;
     result
  END;

CO Print out multifactorials CO

  FOR i TO highest degree DO
     printf (($l, "Degree ", g(0), ":"$, i));
     FOR j TO largest number DO

printf (($xg(0)$, multi fact (j, i)))

     OD
  OD

END </lang>

Output:

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

AutoHotkey

<lang AutoHotkey>Loop, 5 {

   Output .= "Degree " (i := A_Index) ": "
   Loop, 10
       Output .= MultiFact(A_Index, i) (A_Index = 10 ? "`n" : ", ")

} MsgBox, % Output

MultiFact(n, d) {

   Result := n
   while 1 < n -= d
       Result *= n
   return, Result

}</lang> Output:

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

C

Uses: C Runtime (Components:{{#foreach: component$n$|{{{component$n$}}}Property "Uses Library" (as page type) with input value "Library/C Runtime/{{{component$n$}}}" contains invalid characters or is incomplete and therefore can cause unexpected results during a query or annotation process., }})

<lang c> /* Include statements and constant definitions */

  1. include <stdio.h>
  2. define HIGHEST_DEGREE 5
  3. define LARGEST_NUMBER 10

/* Recursive implementation of multifactorial function */ int multifact(int n, int deg){

  return n <= deg ? n : n * multifact(n - deg, deg);

}

/* Iterative implementation of multifactorial function */ int multifact_i(int n, int deg){

  int result = n;
  while (n >= deg + 1){
     result *= (n - deg);
     n -= deg;
  }
  return result;

}

/* Test function to print out multifactorials */ int main(void){

  int i, j;
  for (i = 1; i <= HIGHEST_DEGREE; i++){
     printf("\nDegree %d: ", i);
     for (j = 1; j <= LARGEST_NUMBER; j++){
        printf("%d ", multifact(j, i));
     }
  }

} </lang>

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

C#

<lang csharp>namespace RosettaCode.Multifactorial {

   using System;
   using System.Linq;
   internal static class Program
   {
       private static void Main()
       {
           Console.WriteLine(string.Join(Environment.NewLine,
                                         Enumerable.Range(1, 5)
                                                   .Select(
                                                       degree =>
                                                       string.Join(" ",
                                                                   Enumerable.Range(1, 10)
                                                                             .Select(
                                                                                 number =>
                                                                                 Multifactorial(number, degree))))));
       }
       private static int Multifactorial(int number, int degree)
       {
           if (degree < 1)
           {
               throw new ArgumentOutOfRangeException("degree");
           }
           var count = 1 + (number - 1) / degree;
           if (count < 1)
           {
               throw new ArgumentOutOfRangeException("number");
           }
           return Enumerable.Range(0, count)
                            .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index));
       }
   }

}</lang> 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

C++

<lang cpp>

  1. include <algorithm>
  2. include <iostream>
  3. 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;

} </lang>

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

Clojure

<lang Clojure>(defn !! [m n]

 (->> (iterate #(- % m) n) (take-while pos?) (apply *)))

(doseq [m (range 1 6)]

 (prn m (map #(!! m %) (range 1 11))))</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)

Common Lisp

<lang lisp> (defun mfac (n m)

 (reduce #'* (loop for i from n downto 1 by m collect i)))

(loop for i from 1 to 10

     do (format t "~2@a: ~{~a~^ ~}~%"
                i (loop for j from 1 to 10
                        collect (mfac j i))))

</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

D

<lang d>import std.stdio, std.algorithm, std.range;

T multifactorial(T=long)(in int n, in int m) pure /*nothrow*/ {

   T one = 1;
   return reduce!q{a * b}(one, iota(n, 0, -m));

}

void main() {

   foreach (immutable m; 1 .. 11)
       writefln("%2d: %s", m, iota(1, 11)
                              .map!(n => multifactorial(n, m)));

}</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 

Elixir

Translation of: Erlang

<lang elixir>defmodule RC do

 def multifactorial(n,d) do
   List.foldl(:lists.seq(n,1,-d), 1, fn x,p -> x*p end)
 end

end

Enum.each(1..5, fn d ->

 multifac = Enum.map(1..10, fn n -> RC.multifactorial(n,d) end)
 IO.puts "Degree #{d}: #{inspect multifac}"

end)</lang>

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

Erlang

<lang erlang>-module(multifac). -compile(export_all).

multifac(N,D) ->

   lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)).

main() ->

   Ds = lists:seq(1,5),
   Ns = lists:seq(1,10),   
   lists:foreach(fun (D) ->
                         io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]])
                 end, Ds).</lang>
Output:

<lang erlang>5> multifac:main(). Degree 1: [1,2,6,24,120,720,5040,40320,362880,3628800] Degree 2: [1,2,3,8,15,48,105,384,945,3840] Degree 3: [1,2,3,4,10,18,28,80,162,280] Degree 4: [1,2,3,4,5,12,21,32,45,120] Degree 5: [1,2,3,4,5,6,14,24,36,50] ok</lang>

ERRE

<lang ERRE> PROGRAM MULTIFACTORIAL

PROCEDURE MULTI_FACT(NUM,DEG->MF)

  RESULT=NUM
  N=NUM
  IF N=0 THEN
     MF=1
     EXIT PROCEDURE
  END IF
  LOOP
     N-=DEG
     EXIT IF N<=0
     RESULT*=N
  END LOOP
  MF=RESULT

END PROCEDURE

BEGIN

 PRINT(CHR$(12);)
 FOR DEG=1 TO 10 DO
     PRINT("Degree";DEG;":";)
     FOR NUM=1 TO 10 DO
         MULTI_FACT(NUM,DEG->MF)
         PRINT(MF;)
     END FOR
     PRINT
 END FOR

END PROGRAM </lang>

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

F#

<lang fsharp>let rec mfact d = function

   | n when n <= d   -> n
   | n -> n * mfact d (n-d)

[<EntryPoint>] let main argv =

   let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None
   let (maxDegree, maxN) =
       match argv with
           | [| UInt d; UInt n |] -> (int d, int n)
           | [| UInt d |]         -> (int d, 10)
           | _                    -> (5, 10)
   let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d
   ignore (List.init maxDegree (fun i -> showFor (i+1)))
   0

</lang>

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]

Forth

<lang>: !n negate swap 1 dup rot do i * over +loop nip ;

test cr 6 1 ?do 11 1 ?do i j !n . loop cr loop ;</lang>
Output:
test
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
 ok

FunL

<lang funl>def multifactorial( n, d ) = product( n..1 by -d )

for d <- 1..5

 println( d, [multifactorial(i, d) | i <- 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]

GAP

<lang gap>MultiFactorial := function(n, k)

   local r;
   r := 1;
   while n > 1 do
       r := r*n;
       n := n - k;
   od;
   return r;

end;

PrintArray(List([1 .. 10], n -> List([1 .. 5], k -> MultiFactorial(n, k)))); [ [ 1, 1, 1, 1, 1 ],

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

Go

<lang go>package main

import "fmt"

func multiFactorial(n, k int) int {

   r := 1
   for ; n > 1; n -= k {
       r *= n
   }
   return r

}

func main() {

   for k := 1; k <= 5; k++ {
       fmt.Print("degree ", k, ":")
       for n := 1; n <= 10; n++ {
           fmt.Print(" ", multiFactorial(n, k))
       }
       fmt.Println()
   }

}</lang>

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

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>

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]

Icon and Unicon

The following is Unicon specific but can be readily translated into Icon: <lang unicon>procedure main(A)

   l := integer(A[1]) | 10
   every writeRow(n := !l, [: mf(!10,n) :])

end

procedure writeRow(n, r)

   writes(right(n,3),": ")
   every writes(right(!r,8)|"\n")

end

procedure mf(n, m)

   if n <= 0 then return 1
   return n*mf(n-m, m)

end</lang>

Sample run:

->mf 5
  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
->

J

<lang J>

  NB. tacit implementation of the recursive c function
  NB. int multifact(int n,int deg){return n<=deg?n:n*multifact(n-deg,deg);}
  multifact=: [`([ * - $: ])@.(<~)  
  (a:,<'       degree'),multifact table >:i.10

┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘ </lang>

Java

<lang java>public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; }

public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }</lang>

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

JavaScript

Iterative

Translation of: C

<lang JavaScript> function multifact(n, deg){ var result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } </lang>

<lang JavaScript> function test (n, deg) { for (var i = 1; i <= deg; i ++) { var results = ; for (var j = 1; j <= n; j ++) { results += multifact(j, i) + ' '; } console.log('Degree ' + i + ': ' + results); } } </lang>

Output:

<lang JavaScript> test(10, 5) Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 Degree 2: 1 2 3 8 15 48 105 384 945 3840 Degree 3: 1 2 3 4 10 18 28 80 162 280 Degree 4: 1 2 3 4 5 12 21 32 45 120 Degree 5: 1 2 3 4 5 6 14 24 36 50 </lang>

Recursive

Translation of: C

<lang JavaScript> function multifact(n, deg){ return n <= deg ? n : n * multifact(n - deg, deg); } </lang>

Template:Test <lang JavaScript> function test (n, deg) { for (var i = 1; i <= deg; i ++) { var results = ; for (var j = 1; j <= n; j ++) { results += multifact(j, i) + ' '; } console.log('Degree ' + i + ': ' + results); } } </lang>

Output:

<lang JavaScript> test(10, 5) Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 Degree 2: 1 2 3 8 15 48 105 384 945 3840 Degree 3: 1 2 3 4 10 18 28 80 162 280 Degree 4: 1 2 3 4 5 12 21 32 45 120 Degree 5: 1 2 3 4 5 6 14 24 36 50 </lang>

jq

Works with: jq version 1.4

<lang jq># Input: n

  1. Output: n * (n - d) * (n - 2d) ...

def multifactorial(d):

 . as $n
 | ($n / d | floor) as $k
 | reduce ($n - (d * range(0; $k))) as $i (1; . * $i);</lang>

<lang jq># Print out a d-by-n table of multifactorials neatly: def table(d; n):

 def lpad(i): tostring | (i - length) * " " + .;
 def pp(stream): reduce stream as $i (""; . + ($i | lpad(8)));
 
 range(1; d+1) as $d | "Degree \($d): \( pp(range(1; n+1) | multifactorial($d)) )";</lang>

The specific task: <lang jq>table(5; 10)</lang>

Output:

<lang sh>$ jq -n -r -f Multifactorial.jq Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 Degree 2: 1 2 3 8 15 48 105 384 945 3840 Degree 3: 1 1 3 4 5 18 28 40 162 280 Degree 4: 1 1 1 4 5 6 7 32 45 60 Degree 5: 1 1 1 1 5 6 7 8 9 50</lang>

Julia

multifact calculates the mulitfactorial function iteratively. <lang Julia> function multifact{T<:Integer,U<:Integer}(n::T, k::U)

   -1<n && 0<k || throw(DomainError())
   1 < k || return factorial(n)
   r = one(T)
   for i in n:-k:2
       r *= i
   end
   return r

end

khi = 5 nhi = 10 println("Showing multifactorial for n in [1,", nhi, "] and k in [1,", khi, "].") for k = 1:khi

   a = Int64[multifact(i, k) for i in 1:nhi]
   lab = "n"*"!"^k
   println(@sprintf("  %-6s =>  ", lab), a)

end </lang>

Output:
Showing multifactorial for n in [1,10] and k in [1,5].
  n!     =>  [1,2,6,24,120,720,5040,40320,362880,3628800]
  n!!    =>  [1,2,3,8,15,48,105,384,945,3840]
  n!!!   =>  [1,2,3,4,10,18,28,80,162,280]
  n!!!!  =>  [1,2,3,4,5,12,21,32,45,120]
  n!!!!! =>  [1,2,3,4,5,6,14,24,36,50]

Lua

<lang Lua>function multiFact (n, degree)

   local fact = 1
   for i = n, 2, -degree do
       fact = fact * i
   end
   return fact

end

print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do

   io.write(" " .. d, "\t| ")
   for n = 1, 10 do
       io.write(multiFact(n, d) .. " ")
   end
   print()

end</lang>

Output:
Degree  |       Multifactorials 1 to 10
----------------------------------------------------
 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

Mathematica

<lang mathematica>Multifactorial[n_, m_] := Abs[ Apply[ Times, Range[-n, -1, m]]] Table[ Multifactorial[j, i], {i, 5}, {j, 10}] // TableForm</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

МК-61/52

<lang>П1 <-> П0 П2 ИП0 ИП1 1 + - x>=0 23 ИП2 ИП0 ИП1 - * П2 ИП0 ИП1 - П1 БП 04 ИП2 С/П</lang>

Instruction: number ^ degree В/О С/П

Nim

<lang nim># Recursive proc multifact(n, deg): int =

 result = (if n <= deg: n else: n * multifact(n - deg, deg))
  1. Iterative

proc multifactI(n, deg): int =

 result = n
 var n = n
 while n >= deg + 1:
   result *= n - deg
   n -= deg

for i in 1..5:

 stdout.write "\nDegree ", i, ": "
 for j in 1..10:
   stdout.write multifactI(j, i), " "</lang>

Output:

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

Objeck

Translation of: C

<lang objeck> class Multifact {

  function : MultiFact(n : Int, deg : Int) ~ Int {
     result := n;
     while (n >= deg + 1){
        result *= (n - deg);
        n -= deg;
     };
     return result;
  }
  function : Main(args : String[]) ~ Nil {
     for (i := 1; i <= 5; i+=1;){
        IO.Console->Print("Degree ")->Print(i)->Print(": ");
        for (j := 1; j <= 10; j+=1;){
           IO.Console->Print(' ')->Print(MultiFact(j, i));
        };
        IO.Console->PrintLine();
     };
  }

} </lang>

Output:

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


Oforth

<lang Oforth>func: multifact(deg, n) { 1 while(n 0 > ) [ n * n deg - ->n ] }

func: printMulti { | i |

  5 loop: i [ System.Out i << " : " << 10 seq map(#[ i multifact]) << cr ]

}</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]

PARI/GP

<lang parigp>fac(n,d)=prod(k=0,(n-1)\d,n-k*d) for(k=1,5,for(n=1,10,print1(fac(n,k)" "));print)</lang>

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

Perl

<lang perl>{ # <-- scoping the cache and bigint clause my @cache; use bigint; sub mfact { my ($s, $n) = @_; return 1 if $n <= 0; $cache[$s][$n] //= $n * mfact($s, $n - $s); } }

for my $s (1 .. 10) { print "step=$s: "; print join(" ", map(mfact($s, $_), 1 .. 10)), "\n"; }</lang>

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

Perl 6

<lang perl6>for 1 .. 5 -> $degree {

   sub mfact($n) { [*] $n, *-$degree ...^ * <= 0 };
   say "$degree: ", map &mfact, 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

PL/I

<lang> multi: procedure options (main); /* 29 October 2013 */

  declare (i, j, n) fixed binary;
  declare text character (6) static initial ('n!!!!!');
  do i = 1 to 5;
     put skip edit (substr(text, 1, i+1), '=' ) (A, COLUMN(8));
     do n = 1 to 10;
        put edit ( trim( multifactorial(n,i) ) ) (X(1), A);
     end;
  end;

multifactorial: procedure (n, j) returns (fixed(15));

  declare (n, j) fixed binary;
  declare f fixed (15), m fixed(15);
     f, m = n;
     do while (m > j); f = f * (m-fixed(j)); m = m - j; end;
     return (f);

end multifactorial;

end multi; </lang> Output:

n!     = 1 2 6 24 120 720 5040 40320 362880 3628800
n!!    = 1 2 3 8 15 48 105 384 945 3840
n!!!   = 1 2 3 4 10 18 28 80 162 280
n!!!!  = 1 2 3 4 5 12 21 32 45 120
n!!!!! = 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>

Racket

<lang racket>#lang racket

(define (multi-factorial-fn m)

 (lambda (n)
   (let inner ((acc 1) (n n))
     (if (<= n m) (* acc n)
         (inner (* acc n) (- n m))))))
using (multi-factorial-fn m) as a first-class function

(for*/list ([m (in-range 1 (add1 5))] [mf-m (in-value (multi-factorial-fn m))])

 (for/list ([n (in-range 1 (add1 10))])
 (mf-m n)))

(define (multi-factorial m n) ((multi-factorial-fn m) n))

(for/list ([m (in-range 1 (add1 5))])

 (for/list ([n (in-range 1 (add1 10))])
 (multi-factorial m n)))</lang>

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 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))

REXX

This version also handles zero as well as positive integers. <lang rexx>/*REXX program calculates and displays K-fact (multifactorial) of non-negative integers.*/ numeric digits 1000 /*get ka-razy with the decimal digits. */ parse arg num deg . /*get optional arguments from the C.L. */ if num== | num=="," then num=15 /*Not specified? Then use the default.*/ if deg== | deg=="," then deg=10 /* " " " " " " */ say '═══showing multiple factorials (1 ──►' deg") for numbers 1 ──►" num say

    do d=1  for deg                             /*the factorializing (degree)  of  !'s.*/
    _=                                          /*the list of factorials  (so far).    */
           do f=1  for num                      /* ◄── perform a ! from  1 ───► number.*/
           _=_  Kfact(f, d)                     /*build a  list  of factorial products.*/
           end   /*f*/                          /* [↑]    D   can default to  unity.   */
    say right('n'copies("!", d), 1+deg)    right('['d"]", 2+length(num) )':'     _
    end          /*d*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ Kfact: procedure; !=1; do j=arg(1) to 2 by -word(arg(2) 1,1);  !=!*j; end; return !</lang> output   when using the default input:

═══showing multiple factorials (1 ──► 10)  for numbers  1 ──► 15

         n!  [1]:  1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600 6227020800 87178291200 1307674368000
        n!!  [2]:  1 2 3 8 15 48 105 384 945 3840 10395 46080 135135 645120 2027025
       n!!!  [3]:  1 2 3 4 10 18 28 80 162 280 880 1944 3640 12320 29160
      n!!!!  [4]:  1 2 3 4 5 12 21 32 45 120 231 384 585 1680 3465
     n!!!!!  [5]:  1 2 3 4 5 6 14 24 36 50 66 168 312 504 750
    n!!!!!!  [6]:  1 2 3 4 5 6 7 16 27 40 55 72 91 224 405
   n!!!!!!!  [7]:  1 2 3 4 5 6 7 8 18 30 44 60 78 98 120
  n!!!!!!!!  [8]:  1 2 3 4 5 6 7 8 9 20 33 48 65 84 105
 n!!!!!!!!!  [9]:  1 2 3 4 5 6 7 8 9 10 22 36 52 70 90
n!!!!!!!!!! [10]:  1 2 3 4 5 6 7 8 9 10 11 24 39 56 75

Ring

<lang ring> see "Degree " + "|" + " Multifactorials 1 to 10" + nl see copy("-", 52) + nl for d = 1 to 5

   see "" + d + "       " + "| "
   for n = 1 to 10 
       see "" + multiFact(n, d) + " "
   next
   see nl

next

func multiFact n, degree

    fact = 1
    for i = n to 2 step -degree 
        fact = fact * i
    next
    return fact 

</lang> Output:

Degree  |           Multifactorials 1 to 10
----------------------------------------------------
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

Ruby

<lang ruby> def multifact(n, d)

 n.step(1, -d).inject( :* )

end

(1..5).each {|d| puts "Degree #{d}: #{(1..10).map{|n| multifact(n, d)}.join "\t"}"} </lang> output

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

Scala

<lang scala> def multiFact(n : BigInt, degree : BigInt) = (n to 1 by -degree).product

for{

 degree <- 1 to 5
 str = (1 to 10).map(n => multiFact(n, degree)).mkString(" ")

} println(s"Degree $degree: $str") </lang>

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

Seed7

<lang seed7>$ include "seed7_05.s7i";

const func integer: multiFact (in var integer: num, in integer: degree) is func

 result
   var integer: multiFact is 1;
 begin
   while num > 1 do
     multiFact *:= num;
     num -:= degree;
   end while;
 end func;

const proc: main is func

 local
   var integer: degree is 0;
   var integer: num is 0;
 begin
   for degree range 1 to 5 do
     write("Degree " <& degree <& ": ");
     for num range 1 to 10 do
       write(multiFact(num, degree) <& " ");
     end for;
     writeln;
   end for;
 end func;</lang>
Output:
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 
Degree 2: 1 2 3 8 15 48 105 384 945 3840 
Degree 3: 1 2 3 4 10 18 28 80 162 280 
Degree 4: 1 2 3 4 5 12 21 32 45 120 
Degree 5: 1 2 3 4 5 6 14 24 36 50 

Sidef

<lang ruby>func mfact(s, n) {

   n > 0 ? (n * mfact(s, n-s)) : 1

}   10.times { |s|

   say "step=#{s}: #{1..10 -> map {|n| mfact(s, n)}.join(' ')}"

}</lang>

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

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

VBScript

<lang vb> Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function

For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next </lang>

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

Wortel

<lang wortel>@let {

 facd  &[d n]?{<= n d n @prod@range[n 1 @-d]}
 ; tacit implementation
 facdt ^(!?(/^> .1 ^(@prod @range ~1jdtShj &^!(@- @id))) @,)
 ; recursive
 facdrec &[n d] ?{<= n d n *n !!facdrec -n d d}
 ; output
 l @to 10
 ~@each @to 5 &n !console.log "Degree {n}: {@join @s !*\facd n l}"

}</lang> Output

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

XPL0

<lang XPL0>code ChOut=8, CrLf=9, IntOut=11;

func MultiFac(N, D); \Return multifactorial of N in degree D int N, D; int F; [F:= 1; repeat F:= F*N;

       N:= N-D;

until N <= 1; return F; ];

int I, J; \generate table of multifactorials for J:= 1 to 5 do

   [for I:= 1 to 10 do
       [IntOut(0, MultiFac(I, J));  ChOut(0, 9\tab\)];
   CrLf(0);
   ]</lang>
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      

zkl

<lang zkl>fcn mfact(n,m){ [n..1,-m].reduce('*,1) } foreach m in ([1..5]){ println("%d: %s".fmt(m,[1..10].apply(mfact.fp1(m)))) }</lang>

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