Multifactorial: Difference between revisions
ReeceGoding (talk | contribs) m (→{{header|R}}: Syntax highlighting.) |
m (→{{header|Wren}}: Minor tidy) |
||
(28 intermediate revisions by 16 users not shown) | |||
Line 22: | Line 22: | ||
{{trans|Crystal}} |
{{trans|Crystal}} |
||
< |
<syntaxhighlight lang="11l">F multifact(n, d) |
||
R product((n .< 1).step(-d)) |
R product((n .< 1).step(-d)) |
||
L(d) 1..5 |
L(d) 1..5 |
||
print(‘Degree ’d‘: ’(1..10).map(n -> multifact(n, @d)))</ |
print(‘Degree ’d‘: ’(1..10).map(n -> multifact(n, @d)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 38: | Line 38: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="11l">F multifact(=n, d) |
||
V prod = 1 |
V prod = 1 |
||
L n > 1 |
L n > 1 |
||
prod *= n |
prod *= n |
||
n -= d |
n -= d |
||
R prod</ |
R prod</syntaxhighlight> |
||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
For maximum compatibility, this program uses only the basic instruction set (S/360 1964 POP). |
For maximum compatibility, this program uses only the basic instruction set (S/360 1964 POP). |
||
< |
<syntaxhighlight lang="360asm">* Multifactorial 09/05/2016 |
||
MULFACR CSECT |
MULFACR CSECT |
||
USING MULFACR,13 |
USING MULFACR,13 |
||
Line 97: | Line 97: | ||
R EQU 11 |
R EQU 11 |
||
L EQU 12 |
L EQU 12 |
||
END MULFACR</ |
END MULFACR</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 109: | Line 109: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
||
PROC Multifactorial(INT n,d REAL POINTER res) |
PROC Multifactorial(INT n,d REAL POINTER res) |
||
Line 138: | Line 138: | ||
PutE() |
PutE() |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Multifactorial.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Multifactorial.png Screenshot from Atari 8-bit computer] |
||
Line 150: | Line 150: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Mfact is |
procedure Mfact is |
||
Line 168: | Line 168: | ||
New_line; |
New_line; |
||
end loop; |
end loop; |
||
end Mfact;</ |
end Mfact;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 179: | Line 179: | ||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">mf(integer a, n) |
||
{ |
{ |
||
integer o; |
integer o; |
||
Line 206: | Line 206: | ||
0; |
0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 216: | Line 216: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Translation of C. |
Translation of C. |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
INT highest degree = 5; |
INT highest degree = 5; |
||
INT largest number = 10; |
INT largest number = 10; |
||
Line 240: | Line 240: | ||
OD |
OD |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 253: | Line 253: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Iterative multifactorial based on Ada, AutoHotkey, etc. |
Iterative multifactorial based on Ada, AutoHotkey, etc. |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% returns the multifactorial of n with the specified degree % |
% returns the multifactorial of n with the specified degree % |
||
integer procedure multifactorial ( integer value n, degree ) ; |
integer procedure multifactorial ( integer value n, degree ) ; |
||
Line 274: | Line 274: | ||
end for_v |
end for_v |
||
end for_degree |
end for_degree |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 284: | Line 284: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|AppleScript}}== |
||
<syntaxhighlight lang="applescript">on multifactorial(n, d) |
|||
set f to 1 |
|||
repeat with n from n to 2 by -d |
|||
set f to f * n |
|||
end repeat |
|||
return f |
|||
end multifactorial |
|||
on task() |
|||
Translation of FreeBASIC. |
|||
set table to "" |
|||
repeat with degree from 1 to 5 |
|||
set row to linefeed & "Degree " & degree & ":" |
|||
repeat with n from 1 to 10 |
|||
set row to row & (space & multifactorial(n, degree)) |
|||
end repeat |
|||
set table to table & row |
|||
end repeat |
|||
return table |
|||
end task |
|||
task()</syntaxhighlight> |
|||
<lang ANSI Standard BASIC>100 FUNCTION multiFactorial (n, degree) |
|||
110 IF n < 2 THEN |
|||
{{output}} |
|||
120 LET multiFactorial = 1 |
|||
<syntaxhighlight lang="applescript">" |
|||
130 EXIT FUNCTION |
|||
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
|||
140 END IF |
|||
Degree 2: 1 2 3 8 15 48 105 384 945 3840 |
|||
150 LET result = n |
|||
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"</syntaxhighlight> |
|||
180 NEXT i |
|||
190 LET multiFactorial = result |
|||
200 END FUNCTION |
|||
210 |
|||
220 FOR degree = 1 TO 5 |
|||
230 PRINT "Degree"; degree; " => "; |
|||
240 FOR n = 1 TO 10 |
|||
250 PRINT multiFactorial(n, degree); " "; |
|||
260 NEXT n |
|||
270 PRINT |
|||
280 NEXT degree |
|||
290 END</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">multifact: function [n deg][ |
||
if? n =< deg -> n |
if? n =< deg -> n |
||
else -> n * multifact n-deg deg |
else -> n * multifact n-deg deg |
||
Line 321: | Line 327: | ||
] |
] |
||
print "" |
print "" |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 332: | Line 338: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">Loop, 5 { |
||
Output .= "Degree " (i := A_Index) ": " |
Output .= "Degree " (i := A_Index) ": " |
||
Loop, 10 |
Loop, 10 |
||
Line 344: | Line 350: | ||
Result *= n |
Result *= n |
||
return, Result |
return, Result |
||
}</ |
}</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre>Degree 1: 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 |
<pre>Degree 1: 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 |
||
Line 353: | Line 359: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f MULTIFACTORIAL.AWK |
# syntax: GAWK -f MULTIFACTORIAL.AWK |
||
# converted from Go |
# converted from Go |
||
Line 373: | Line 379: | ||
return(r) |
return(r) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 383: | Line 389: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|BASIC}}== |
||
==={{header|ANSI BASIC}}=== |
|||
<lang bbcbasic>REM >multifact |
|||
{{trans|FreeBASIC}} |
|||
{{works with|Decimal BASIC}} |
|||
<syntaxhighlight lang="basic">100 FUNCTION multiFactorial (n, degree) |
|||
110 IF n < 2 THEN |
|||
120 LET multiFactorial = 1 |
|||
130 EXIT FUNCTION |
|||
140 END IF |
|||
150 LET result = n |
|||
160 FOR i = n - degree TO 2 STEP -degree |
|||
170 LET result = result * i |
|||
180 NEXT i |
|||
190 LET multiFactorial = result |
|||
200 END FUNCTION |
|||
210 |
|||
220 FOR degree = 1 TO 5 |
|||
230 PRINT "Degree"; degree; " => "; |
|||
240 FOR n = 1 TO 10 |
|||
250 PRINT multiFactorial(n, degree); " "; |
|||
260 NEXT n |
|||
270 PRINT |
|||
280 NEXT degree |
|||
290 END</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
==={{header|BBC BASIC}}=== |
|||
<syntaxhighlight lang="bbcbasic">REM >multifact |
|||
FOR i% = 1 TO 5 |
FOR i% = 1 TO 5 |
||
PRINT "Degree "; i%; ":"; |
PRINT "Degree "; i%; ":"; |
||
Line 400: | Line 439: | ||
mfact% = mfact% * i% |
mfact% = mfact% * i% |
||
NEXT |
NEXT |
||
= mfact%</ |
= mfact%</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 407: | Line 446: | ||
Degree 4: 1 2 3 4 5 12 21 32 45 120 |
Degree 4: 1 2 3 4 5 12 21 32 45 120 |
||
Degree 5: 1 2 3 4 5 6 14 24 36 50</pre> |
Degree 5: 1 2 3 4 5 6 14 24 36 50</pre> |
||
==={{header|IS-BASIC}}=== |
|||
<syntaxhighlight lang="is-basic">100 PROGRAM "MultiFac.bas" |
|||
110 FOR I=1 TO 5 |
|||
120 PRINT "Degree";I;": "; |
|||
130 FOR J=1 TO 10 |
|||
140 PRINT MFACT(J,I);" "; |
|||
150 NEXT |
|||
160 PRINT |
|||
170 NEXT |
|||
180 DEF MFACT(N,DEGREE) |
|||
190 LET RESULT=1 |
|||
200 FOR X=N TO 1 STEP-DEGREE |
|||
210 LET RESULT=RESULT*X |
|||
220 NEXT |
|||
230 LET MFACT=RESULT |
|||
240 END DEF</syntaxhighlight> |
|||
=={{header|BQN}}== |
|||
<syntaxhighlight lang="bqn">MultiFact ← ×´⊣-↕∘⌈∘÷×⊢ |
|||
MultiFact⌜⟜(5⊸↑) 1+↕10</syntaxhighlight> |
|||
{{out}} |
|||
<pre>┌─ |
|||
╵ 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 |
|||
┘</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
{{uses from|Library|C Runtime|component1=printf}} |
{{uses from|Library|C Runtime|component1=printf}} |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
/* Include statements and constant definitions */ |
/* Include statements and constant definitions */ |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 441: | Line 515: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 452: | Line 526: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp">namespace RosettaCode.Multifactorial |
||
{ |
{ |
||
using System; |
using System; |
||
Line 489: | Line 563: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 498: | Line 572: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <algorithm> |
#include <algorithm> |
||
#include <iostream> |
#include <iostream> |
||
Line 515: | Line 589: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 531: | Line 605: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn !! [m n] |
||
(->> (iterate #(- % m) n) (take-while pos?) (apply *))) |
(->> (iterate #(- % m) n) (take-while pos?) (apply *))) |
||
(doseq [m (range 1 6)] |
(doseq [m (range 1 6)] |
||
(prn m (map #(!! m %) (range 1 11))))</ |
(prn m (map #(!! m %) (range 1 11))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 546: | Line 620: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">multifactorial = proc (n, degree: int) returns (int) |
||
result: int := 1 |
result: int := 1 |
||
for i: int in int$from_to_by(n, 1, -degree) do |
for i: int in int$from_to_by(n, 1, -degree) do |
||
Line 562: | Line 636: | ||
stream$putc(po, '\n') |
stream$putc(po, '\n') |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 1 1 1 1 |
<pre> 1 1 1 1 1 |
||
Line 576: | Line 650: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun mfac (n m) |
(defun mfac (n m) |
||
(reduce #'* (loop for i from n downto 1 by m collect i))) |
(reduce #'* (loop for i from n downto 1 by m collect i))) |
||
Line 584: | Line 658: | ||
i (loop for j from 1 to 10 |
i (loop for j from 1 to 10 |
||
collect (mfac j i)))) |
collect (mfac j i)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 597: | Line 671: | ||
9: 1 2 3 4 5 6 7 8 9 10 |
9: 1 2 3 4 5 6 7 8 9 10 |
||
10: 1 2 3 4 5 6 7 8 9 10</pre> |
10: 1 2 3 4 5 6 7 8 9 10</pre> |
||
=={{header|Cowgol}}== |
|||
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
|||
sub multifac(n: uint32, d: uint32): (r: uint32) is |
|||
r := 1; |
|||
loop |
|||
r := r * n; |
|||
if n <= d then break; end if; |
|||
n := n - d; |
|||
end loop; |
|||
end sub; |
|||
var d: uint32 := 1; |
|||
while d <= 5 loop |
|||
print_i32(d); |
|||
print(": "); |
|||
var n: uint32 := 1; |
|||
while n <= 10 loop |
|||
print_i32(multifac(n, d)); |
|||
print(" "); |
|||
n := n + 1; |
|||
end loop; |
|||
print_nl(); |
|||
d := d + 1; |
|||
end loop;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>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</pre> |
|||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">def multifact(n, d) |
||
n.step(to: 1, by: -d).product |
n.step(to: 1, by: -d).product |
||
end |
end |
||
(1..5).each {|d| puts "Degree #{d}: #{(1..10).map{|n| multifact(n, d)}.join "\t"}"}</ |
(1..5).each {|d| puts "Degree #{d}: #{(1..10).map{|n| multifact(n, d)}.join "\t"}"}</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |
||
Line 614: | Line 720: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range; |
||
T multifactorial(T=long)(in int n, in int m) pure /*nothrow*/ { |
T multifactorial(T=long)(in int n, in int m) pure /*nothrow*/ { |
||
Line 625: | Line 731: | ||
writefln("%2d: %s", m, iota(1, 11) |
writefln("%2d: %s", m, iota(1, 11) |
||
.map!(n => multifactorial(n, m))); |
.map!(n => multifactorial(n, m))); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre> 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 639: | Line 745: | ||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
< |
<syntaxhighlight lang="dart"> |
||
main() |
main() |
||
{ |
{ |
||
Line 665: | Line 771: | ||
return a*fact((a-b),b); |
return a*fact((a-b),b); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Draco}}== |
|||
<syntaxhighlight lang="draco">proc nonrec multifac(int n, deg) ulong: |
|||
ulong result; |
|||
result := 1; |
|||
while n > 1 do |
|||
result := result * n; |
|||
n := n - deg |
|||
od; |
|||
result |
|||
corp |
|||
proc nonrec main() void: |
|||
byte n, d; |
|||
for n from 1 upto 10 do |
|||
for d from 1 upto 5 do |
|||
write(multifac(n,d):10) |
|||
od; |
|||
writeln() |
|||
od |
|||
corp</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 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</pre> |
|||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
function MultiFact(Num,Deg: integer): integer; |
|||
{Multifactorial from Degree and Number} |
|||
var N: integer; |
|||
begin |
|||
N:=Num; |
|||
Result:=Num; |
|||
if N = 0 then Result:=1 |
|||
else while true do |
|||
begin |
|||
N := N - deg; |
|||
if N<1 then break; |
|||
Result:=Result * N; |
|||
end; |
|||
end; |
|||
procedure ShowMultifactorial(Memo: TMemo); |
|||
{Show combinations of deg/num of multifactorial} |
|||
var Deg,Num: integer; |
|||
var S: string; |
|||
begin |
|||
S:=''; |
|||
for Deg:=1 to 5 do |
|||
begin |
|||
S:=S+Format('Degree: %d:',[Deg]); |
|||
for Num:=1 to 10 do S:=S+' '+Format('%7d',[MultiFact(Num,Deg)]); |
|||
S:=S+#$0D#$0A; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
=={{header|EasyLang}}== |
|||
{{trans|AWK}} |
|||
<syntaxhighlight lang=easylang> |
|||
func mfact n k . |
|||
r = 1 |
|||
while n > 1 |
|||
r *= n |
|||
n -= k |
|||
. |
|||
return r |
|||
. |
|||
for k = 1 to 5 |
|||
write "degree " & k & ":" |
|||
for n = 1 to 10 |
|||
write " " & mfact n k |
|||
. |
|||
print "" |
|||
. |
|||
</syntaxhighlight> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Erlang}} |
{{trans|Erlang}} |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def multifactorial(n,d) do |
def multifactorial(n,d) do |
||
Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) |
Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) |
||
Line 678: | Line 884: | ||
multifac = for n <- 1..10, do: RC.multifactorial(n,d) |
multifac = for n <- 1..10, do: RC.multifactorial(n,d) |
||
IO.puts "Degree #{d}: #{inspect multifac}" |
IO.puts "Degree #{d}: #{inspect multifac}" |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 690: | Line 896: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">-module(multifac). |
||
-compile(export_all). |
-compile(export_all). |
||
Line 701: | Line 907: | ||
lists:foreach(fun (D) -> |
lists:foreach(fun (D) -> |
||
io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) |
io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) |
||
end, Ds).</ |
end, Ds).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="erlang">5> multifac:main(). |
||
Degree 1: [1,2,6,24,120,720,5040,40320,362880,3628800] |
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 2: [1,2,3,8,15,48,105,384,945,3840] |
||
Line 709: | Line 915: | ||
Degree 4: [1,2,3,4,5,12,21,32,45,120] |
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 5: [1,2,3,4,5,6,14,24,36,50] |
||
ok</ |
ok</syntaxhighlight> |
||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM MULTIFACTORIAL |
PROGRAM MULTIFACTORIAL |
||
Line 741: | Line 947: | ||
END FOR |
END FOR |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Degree 1 : 1 2 6 24 120 720 5040 40320 362880 3628800 |
Degree 1 : 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 757: | Line 963: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">let rec mfact d = function |
||
| n when n <= d -> n |
| n when n <= d -> n |
||
| n -> n * mfact d (n-d) |
| n -> n * mfact d (n-d) |
||
Line 772: | Line 978: | ||
ignore (List.init maxDegree (fun i -> showFor (i+1))) |
ignore (List.init maxDegree (fun i -> showFor (i+1))) |
||
0 |
0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre>1: [1; 2; 6; 24; 120; 720; 5040; 40320; 362880; 3628800] |
<pre>1: [1; 2; 6; 24; 120; 720; 5040; 40320; 362880; 3628800] |
||
2: [1; 2; 3; 8; 15; 48; 105; 384; 945; 3840] |
2: [1; 2; 3; 8; 15; 48; 105; 384; 945; 3840] |
||
Line 780: | Line 986: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang>USING: formatting io kernel math math.ranges prettyprint |
<syntaxhighlight lang="text">USING: formatting io kernel math math.ranges prettyprint |
||
sequences ; |
sequences ; |
||
IN: rosetta-code.multifactorial |
IN: rosetta-code.multifactorial |
||
Line 794: | Line 1,000: | ||
5 [1,b] [ mf-row nl ] each ; |
5 [1,b] [ mf-row nl ] each ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 805: | Line 1,011: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang>: !n negate swap 1 dup rot do i * over +loop nip ; |
<syntaxhighlight lang="text">: !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 ;</ |
: test cr 6 1 ?do 11 1 ?do i j !n . loop cr loop ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>test |
<pre>test |
||
Line 818: | Line 1,024: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|95 and later}} |
{{works with|Fortran|95 and later}} |
||
< |
<syntaxhighlight lang="fortran">program test |
||
implicit none |
implicit none |
||
integer :: i, j, n |
integer :: i, j, n |
||
Line 840: | Line 1,046: | ||
end function multifactorial |
end function multifactorial |
||
end program test</ |
end program test</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 851: | Line 1,057: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Function multiFactorial (n As UInteger, degree As Integer) As UInteger |
Function multiFactorial (n As UInteger, degree As Integer) As UInteger |
||
Line 872: | Line 1,078: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 884: | Line 1,090: | ||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
< |
<syntaxhighlight lang="funl">def multifactorial( n, d ) = product( n..1 by -d ) |
||
for d <- 1..5 |
for d <- 1..5 |
||
println( d, [multifactorial(i, d) | i <- 1..10] ))</ |
println( d, [multifactorial(i, d) | i <- 1..10] ))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 900: | Line 1,106: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap">MultiFactorial := function(n, k) |
||
local r; |
local r; |
||
r := 1; |
r := 1; |
||
Line 920: | Line 1,126: | ||
[ 40320, 384, 80, 32, 24 ], |
[ 40320, 384, 80, 32, 24 ], |
||
[ 362880, 945, 162, 45, 36 ], |
[ 362880, 945, 162, 45, 36 ], |
||
[ 3628800, 3840, 280, 120, 50 ] ]</ |
[ 3628800, 3840, 280, 120, 50 ] ]</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 943: | Line 1,149: | ||
fmt.Println() |
fmt.Println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 954: | Line 1,160: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">mulfac :: (Num a, Enum a) => a -> [a] |
||
mulfac k = 1 : s |
mulfac k = 1 : s |
||
where |
where |
||
Line 969: | Line 1,175: | ||
(print . take 10 . tail . mulfac) |
(print . take 10 . tail . mulfac) |
||
[1 .. 5] |
[1 .. 5] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[1,2,6,24,120,720,5040,40320,362880,3628800] |
<pre>[1,2,6,24,120,720,5040,40320,362880,3628800] |
||
Line 980: | Line 1,186: | ||
The following is Unicon specific but can be readily translated into Icon: |
The following is Unicon specific but can be readily translated into Icon: |
||
< |
<syntaxhighlight lang="unicon">procedure main(A) |
||
l := integer(A[1]) | 10 |
l := integer(A[1]) | 10 |
||
every writeRow(n := !l, [: mf(!10,n) :]) |
every writeRow(n := !l, [: mf(!10,n) :]) |
||
Line 993: | Line 1,199: | ||
if n <= 0 then return 1 |
if n <= 0 then return 1 |
||
return n*mf(n-m, m) |
return n*mf(n-m, m) |
||
end</ |
end</syntaxhighlight> |
||
Sample run: |
Sample run: |
||
Line 1,007: | Line 1,213: | ||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
< |
<syntaxhighlight lang="is-basic">100 PROGRAM "Multifac.bas" |
||
110 FOR I=1 TO 5 |
110 FOR I=1 TO 5 |
||
120 PRINT "Degree";I;":"; |
120 PRINT "Degree";I;":"; |
||
Line 1,023: | Line 1,229: | ||
240 NEXT |
240 NEXT |
||
250 LET MFACT=RES |
250 LET MFACT=RES |
||
260 END DEF</ |
260 END DEF</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> NB. n multifact degree |
|||
<lang J> |
|||
multifact=: */@([ - ] * i.@>.@%)&> |
|||
NB. tacit implementation of the recursive c function |
|||
('';' degree'),multifact table >:i.10 |
|||
NB. int multifact(int n,int deg){return n<=deg?n:n*multifact(n-deg,deg);} |
|||
multifact=: [`([ * - $: ])@.(<~) |
|||
(a:,<' degree'),multifact table >:i.10 |
|||
┌─────────┬──────────────────────────────────────┐ |
┌─────────┬──────────────────────────────────────┐ |
||
│ │ degree │ |
│ │ degree │ |
||
Line 1,047: | Line 1,250: | ||
│ 9 │ 362880 945 162 45 36 27 18 9 9 9│ |
│ 9 │ 362880 945 162 45 36 27 18 9 9 9│ |
||
│10 │3628800 3840 280 120 50 40 30 20 10 10│ |
│10 │3628800 3840 280 120 50 40 30 20 10 10│ |
||
└─────────┴──────────────────────────────────────┘ |
└─────────┴──────────────────────────────────────┘</syntaxhighlight> |
||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">public class MultiFact { |
||
private static long multiFact(long n, int deg){ |
private static long multiFact(long n, int deg){ |
||
long ans = 1; |
long ans = 1; |
||
Line 1,069: | Line 1,271: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,081: | Line 1,283: | ||
===Iterative=== |
===Iterative=== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
function multifact(n, deg){ |
function multifact(n, deg){ |
||
var result = n; |
var result = n; |
||
Line 1,090: | Line 1,292: | ||
return result; |
return result; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
function test (n, deg) { |
function test (n, deg) { |
||
for (var i = 1; i <= deg; i ++) { |
for (var i = 1; i <= deg; i ++) { |
||
Line 1,102: | Line 1,304: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
test(10, 5) |
test(10, 5) |
||
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,112: | Line 1,314: | ||
Degree 4: 1 2 3 4 5 12 21 32 45 120 |
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 5: 1 2 3 4 5 6 14 24 36 50 |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Recursive=== |
===Recursive=== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="javascript">function multifact(n, deg){ |
||
return n <= deg ? n : n * multifact(n - deg, deg); |
return n <= deg ? n : n * multifact(n - deg, deg); |
||
}</ |
}</syntaxhighlight> |
||
Test |
Test |
||
< |
<syntaxhighlight lang="javascript">function test (n, deg) { |
||
for (var i = 1; i <= deg; i ++) { |
for (var i = 1; i <= deg; i ++) { |
||
var results = ''; |
var results = ''; |
||
Line 1,130: | Line 1,332: | ||
console.log('Degree ' + i + ': ' + results); |
console.log('Degree ' + i + ': ' + results); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
test(10, 5) |
test(10, 5) |
||
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,138: | Line 1,340: | ||
Degree 3: 1 2 3 4 10 18 28 80 162 280 |
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 4: 1 2 3 4 5 12 21 32 45 120 |
||
Degree 5: 1 2 3 4 5 6 14 24 36 50 </ |
Degree 5: 1 2 3 4 5 6 14 24 36 50 </syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
< |
<syntaxhighlight lang="jq"># Input: n |
||
# Output: n * (n - d) * (n - 2d) ... |
# Output: n * (n - d) * (n - 2d) ... |
||
def multifactorial(d): |
def multifactorial(d): |
||
. as $n |
. as $n |
||
| ($n / d | floor) as $k |
| ($n / d | floor) as $k |
||
| reduce ($n - (d * range(0; $k))) as $i (1; . * $i);</ |
| reduce ($n - (d * range(0; $k))) as $i (1; . * $i);</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq"># Print out a d-by-n table of multifactorials neatly: |
||
def table(d; n): |
def table(d; n): |
||
def lpad(i): tostring | (i - length) * " " + .; |
def lpad(i): tostring | (i - length) * " " + .; |
||
def pp(stream): reduce stream as $i (""; . + ($i | lpad(8))); |
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)) )";</ |
range(1; d+1) as $d | "Degree \($d): \( pp(range(1; n+1) | multifactorial($d)) )";</syntaxhighlight> |
||
The specific task: |
The specific task: |
||
<lang |
<syntaxhighlight lang="jq">table(5; 10)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">$ jq -n -r -f Multifactorial.jq |
||
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
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 2: 1 2 3 8 15 48 105 384 945 3840 |
||
Degree 3: 1 1 3 4 5 18 28 40 162 280 |
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 4: 1 1 1 4 5 6 7 32 45 60 |
||
Degree 5: 1 1 1 1 5 6 7 8 9 50</ |
Degree 5: 1 1 1 1 5 6 7 8 9 50</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Printf |
||
function multifact(n::Integer, k::Integer) |
function multifact(n::Integer, k::Integer) |
||
Line 1,181: | Line 1,383: | ||
lab = "n" * "!" ^ k |
lab = "n" * "!" ^ k |
||
@printf(" %-6s → %s\n", lab, a) |
@printf(" %-6s → %s\n", lab, a) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,192: | Line 1,394: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">fun multifactorial(n: Long, d: Int) : Long { |
||
val r = n % d |
val r = n % d |
||
return (1..n).filter { it % d == r } .reduce { i, p -> i * p } |
return (1..n).filter { it % d == r } .reduce { i, p -> i * p } |
||
Line 1,205: | Line 1,407: | ||
println() |
println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> !: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre> !: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,214: | Line 1,416: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
{def multifact |
{def multifact |
||
{lambda {:n :deg} |
{lambda {:n :deg} |
||
Line 1,232: | Line 1,434: | ||
Degree 4: 1 2 3 4 5 12 21 32 45 120 |
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 5: 1 2 3 4 5 6 14 24 36 50 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Latitude}}== |
=={{header|Latitude}}== |
||
< |
<syntaxhighlight lang="latitude">use 'format importAllSigils. |
||
multiFactorial := { |
multiFactorial := { |
||
Line 1,246: | Line 1,448: | ||
answers := 1 upto 11 to (Array) map { multiFactorial ($1, degree). }. |
answers := 1 upto 11 to (Array) map { multiFactorial ($1, degree). }. |
||
$stdout printf: ~fmt "Degree ~S: ~S", degree, answers. |
$stdout printf: ~fmt "Degree ~S: ~S", degree, answers. |
||
}.</ |
}.</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,256: | Line 1,458: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function multiFact (n, degree) |
||
local fact = 1 |
local fact = 1 |
||
for i = n, 2, -degree do |
for i = n, 2, -degree do |
||
Line 1,272: | Line 1,474: | ||
end |
end |
||
print() |
print() |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Degree | Multifactorials 1 to 10 |
<pre>Degree | Multifactorials 1 to 10 |
||
Line 1,284: | Line 1,486: | ||
=={{header|MAD}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
INTERNAL FUNCTION(N,DEG) |
INTERNAL FUNCTION(N,DEG) |
||
Line 1,299: | Line 1,501: | ||
VECTOR VALUES OUTP = $5(I10,S1)*$ |
VECTOR VALUES OUTP = $5(I10,S1)*$ |
||
END OF PROGRAM</ |
END OF PROGRAM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,316: | Line 1,518: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang="maple">f := proc (n, m) |
|||
{{output?|Maple}} |
|||
<lang Maple> |
|||
f := proc (n, m) |
|||
local fac, i; |
local fac, i; |
||
fac := 1; |
fac := 1; |
||
Line 1,333: | Line 1,533: | ||
end do; |
end do; |
||
end do; |
end do; |
||
a;</syntaxhighlight> |
|||
a; |
|||
{{out}} |
|||
</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] |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] |
||
Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm</ |
Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,345: | Line 1,554: | ||
4: 1 2 3 4 5 12 21 32 45 120 |
4: 1 2 3 4 5 12 21 32 45 120 |
||
5: 1 2 3 4 5 6 14 24 36 50</pre> |
5: 1 2 3 4 5 6 14 24 36 50</pre> |
||
=={{header|Maxima}}== |
|||
Using built-in function genfact |
|||
<syntaxhighlight lang="maxima"> |
|||
multifactorial(x,n):=genfact(x,x/n,n)$ |
|||
/* Test case */ |
|||
makelist(multifactorial(i,1),i,1,10); |
|||
makelist(multifactorial(i,2),i,1,10); |
|||
block(makelist(mod(i,3),i,1,10),at(%%,0=1),%%*makelist(multifactorial(i,3),i,1,10)); |
|||
block(makelist(mod(i,4),i,1,10),at(%%,0=1),%%*makelist(multifactorial(i,4),i,1,10)); |
|||
block(makelist(mod(i,5),i,1,10),at(%%,0=1),%%*makelist(multifactorial(i,5),i,1,10)); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[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] |
|||
</pre> |
|||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
< |
<syntaxhighlight lang="min">(:d (dup 0 <=) (pop 1) (dup d -) (*) linrec) :multifactorial |
||
(:d 1 (dup d multifactorial print! " " print! succ) 10 times newline pop) :row |
(:d 1 (dup d multifactorial print! " " print! succ) 10 times newline pop) :row |
||
1 (dup "Degree " print! print ": " print! row succ) 5 times</ |
1 (dup "Degree " print! print ": " print! row succ) 5 times</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,360: | Line 1,590: | ||
Degree 5: 1 2 3 4 5 6 14 24 36 50 |
Degree 5: 1 2 3 4 5 6 14 24 36 50 |
||
</pre> |
</pre> |
||
=={{header|Miranda}}== |
|||
<syntaxhighlight lang="miranda">main :: [sys_message] |
|||
main = [ Stdout (show deg ++ ": " ++ show (map (multifac deg) [1..10]) ++ "\n") |
|||
| deg <- [1..5]] |
|||
multifac :: num->num->num |
|||
multifac deg = product . takewhile (>1) . iterate sub |
|||
where sub n = n - deg</syntaxhighlight> |
|||
{{out}} |
|||
<pre>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]</pre> |
|||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>П1 <-> П0 П2 ИП0 ИП1 1 + - x>=0 |
<syntaxhighlight lang="text">П1 <-> П0 П2 ИП0 ИП1 1 + - x>=0 |
||
23 ИП2 ИП0 ИП1 - * П2 ИП0 ИП1 - |
23 ИП2 ИП0 ИП1 - * П2 ИП0 ИП1 - |
||
П1 БП 04 ИП2 С/П</ |
П1 БП 04 ИП2 С/П</syntaxhighlight> |
||
Instruction: ''number'' ^ ''degree'' В/О С/П |
Instruction: ''number'' ^ ''degree'' В/О С/П |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim"># Recursive |
||
proc multifact(n, deg: int): int = |
proc multifact(n, deg: int): int = |
||
result = (if n <= deg: n else: n * multifact(n - deg, deg)) |
result = (if n <= deg: n else: n * multifact(n - deg, deg)) |
||
Line 1,385: | Line 1,630: | ||
for j in 1..10: |
for j in 1..10: |
||
stdout.write multifactI(j, i), " " |
stdout.write multifactI(j, i), " " |
||
stdout.write('\n')</ |
stdout.write('\n')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,396: | Line 1,641: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="objeck"> |
||
class Multifact { |
class Multifact { |
||
function : MultiFact(n : Int, deg : Int) ~ Int { |
function : MultiFact(n : Int, deg : Int) ~ Int { |
||
Line 1,418: | Line 1,663: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 1,427: | Line 1,672: | ||
Degree 4: 1 2 3 4 5 12 21 32 45 120 |
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 5: 1 2 3 4 5 6 14 24 36 50 |
||
</pre> |
|||
=={{header|OCaml}}== |
|||
<syntaxhighlight lang="ocaml">let multi_fac d n = |
|||
let rec loop a x = if x < 2 then a else loop (a * x) (x - d) in |
|||
loop n (n - d) |
|||
let () = |
|||
for i = 1 to 5 do |
|||
Seq.(ints 1 |> take 10 |> map (multi_fac i) |> map string_of_int) |
|||
|> List.of_seq |> String.concat " " |> print_endline |
|||
done</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
</pre> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: multifact(n, deg) 1 while( n 0 > ) [ n * n deg - ->n ] ; |
||
: printMulti |
: printMulti |
||
| i | |
| i | |
||
5 loop: i [ System.Out i << " : " << 10 seq map(#[ i multifact]) << cr ] ;</ |
5 loop: i [ System.Out i << " : " << 10 seq map(#[ i multifact]) << cr ] ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,444: | Line 1,708: | ||
4 : [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] |
4 : [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] |
||
5 : [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] |
5 : [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] |
||
</pre> |
|||
=={{header|Ol}}== |
|||
<syntaxhighlight lang="scheme"> |
|||
(define (multifactorial n d) |
|||
(fold * 1 (iota (div n d) n (negate d)))) |
|||
(for-each (lambda (i) |
|||
(display "Degree ") |
|||
(display i) |
|||
(display ":") |
|||
(for-each (lambda (n) |
|||
(display " ") |
|||
(display (multifactorial n i))) |
|||
(iota 10 1)) |
|||
(print)) |
|||
(iota 5 1)) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
By the way, we can create few multifactorial functions and use them directly or as part of infix math notation (inside "//" macro). |
|||
<syntaxhighlight lang="scheme"> |
|||
(define (!!!!! n) (multifactorial n 5)) |
|||
(print (!!!!! 74)) |
|||
(import (math infix-notation)) |
|||
; register !!!!! as a postfix function |
|||
(define \\postfix-functions (put \\postfix-functions '!!!!! #t)) |
|||
; now use "\\" as usual |
|||
(print (\\ |
|||
2 + 74!!!!! |
|||
)) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
4959435223298761261056 |
|||
4959435223298761261058 |
|||
</pre> |
</pre> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight 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)</ |
for(k=1,5,for(n=1,10,print1(fac(n,k)" "));print)</syntaxhighlight> |
||
<pre>1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>1 2 6 24 120 720 5040 40320 362880 3628800 |
||
1 2 3 8 15 48 105 384 945 3840 |
1 2 3 8 15 48 105 384 945 3840 |
||
Line 1,456: | Line 1,765: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">{ # <-- scoping the cache and bigint clause |
||
my @cache; |
my @cache; |
||
use bigint; |
use bigint; |
||
Line 1,469: | Line 1,778: | ||
print "step=$s: "; |
print "step=$s: "; |
||
print join(" ", map(mfact($s, $_), 1 .. 10)), "\n"; |
print join(" ", map(mfact($s, $_), 1 .. 10)), "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,486: | Line 1,795: | ||
We can also do this iteratively. ntheory's vecprod makes bigint products if needed, so we don't have to worry about it. |
We can also do this iteratively. ntheory's vecprod makes bigint products if needed, so we don't have to worry about it. |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use ntheory qw/vecprod/; |
||
sub mfac { |
sub mfac { |
||
Line 1,495: | Line 1,804: | ||
for my $degree (1..5) { |
for my $degree (1..5) { |
||
say "$degree: ",join(" ",map{mfac($_,$degree)} 1..10); |
say "$degree: ",join(" ",map{mfac($_,$degree)} 1..10); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,504: | Line 1,813: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<lang Phix>function multifactorial(integer n, integer order) |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
atom res = 1 |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">multifactorial</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">)</span> |
|||
if n>0 then |
|||
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
res = n*multifactorial(n-order,order) |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> |
|||
end if |
|||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">multifactorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">order</span><span style="color: #0000FF;">)</span> |
|||
return res |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end function |
|||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
sequence s = repeat(0,10) |
|||
for i=1 to 5 do |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> |
|||
for j=1 to 10 do |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span> |
|||
s[j] = multifactorial(j,i) |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span> |
|||
end for |
|||
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">multifactorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
|||
?s |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end for</lang> |
|||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,527: | Line 1,839: | ||
{1,2,3,4,5,6,14,24,36,50} |
{1,2,3,4,5,6,14,24,36,50} |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
|||
===Using prod/1=== |
|||
<syntaxhighlight lang="picat">multifactorial(N,Degree) = prod([ I : I in N..-Degree..1]).</syntaxhighlight> |
|||
===Using reduce/2=== |
|||
<syntaxhighlight lang="picat">multifactorial2(N,Degree) = reduce(*, [I : I in N..-Degree..1]).</syntaxhighlight> |
|||
===While loop=== |
|||
<syntaxhighlight lang="picat">multifactorial3(N,Degree) = M => |
|||
M = 1, I = N, |
|||
while(I > 0) |
|||
M := M*I, |
|||
I := I - Degree |
|||
end.</syntaxhighlight> |
|||
===Recursive variants=== |
|||
<syntaxhighlight lang="picat">multifactorial4(N,_D) = 1, N <= 0 => true.</syntaxhighlight> |
|||
<syntaxhighlight lang="picat">multifactorial4(N,D) = N*multifactorial4(N-D,D).</syntaxhighlight> |
|||
<syntaxhighlight lang="picat">multifactorial5(N,D) = M => |
|||
N <= 0 -> M = 1 ; M = N*multifactorial4(N-D,D).</syntaxhighlight> |
|||
<syntaxhighlight lang="picat">multifactorial6(N,D) = cond(N <= 0, 1, N*multifactorial6(N-D,D)).</syntaxhighlight> |
|||
===Test=== |
|||
<syntaxhighlight lang="picat">import util. |
|||
go => |
|||
foreach(D in 1..15) |
|||
println(D=[multifactorial(I,D) : I in 1..15]) |
|||
end, |
|||
nl.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1 = [1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000] |
|||
2 = [1,2,3,8,15,48,105,384,945,3840,10395,46080,135135,645120,2027025] |
|||
3 = [1,2,3,4,10,18,28,80,162,280,880,1944,3640,12320,29160] |
|||
4 = [1,2,3,4,5,12,21,32,45,120,231,384,585,1680,3465] |
|||
5 = [1,2,3,4,5,6,14,24,36,50,66,168,312,504,750] |
|||
6 = [1,2,3,4,5,6,7,16,27,40,55,72,91,224,405] |
|||
7 = [1,2,3,4,5,6,7,8,18,30,44,60,78,98,120] |
|||
8 = [1,2,3,4,5,6,7,8,9,20,33,48,65,84,105] |
|||
9 = [1,2,3,4,5,6,7,8,9,10,22,36,52,70,90] |
|||
10 = [1,2,3,4,5,6,7,8,9,10,11,24,39,56,75] |
|||
11 = [1,2,3,4,5,6,7,8,9,10,11,12,26,42,60] |
|||
12 = [1,2,3,4,5,6,7,8,9,10,11,12,13,28,45] |
|||
13 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,30] |
|||
14 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] |
|||
15 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]</pre> |
|||
===Constraint modelling=== |
|||
Using constraint modelling for a reversible variant (i.e. all parameters can be inputs or outputs); here shown by identifying all the valid N and the degree given the multifactorial (M). |
|||
<syntaxhighlight lang="picat">import cp. |
|||
% |
|||
% Reversible: find Degree and N given M |
|||
% |
|||
go2 => |
|||
Ms = [4,20,105], % The multifactorials to identify |
|||
foreach(M in Ms) |
|||
println(m=M), |
|||
Degree :: 1..10, % limit of the degree |
|||
N :: 1..100, % limit of N |
|||
All = findall([N,Degree,M], (multifactorial_reversible(N,Degree,M), |
|||
solve([M,N,Degree]))), |
|||
foreach([NN,DD,MM] in All.sort) |
|||
printf("n=%d degree=%d m=%d\n",NN,DD,MM) |
|||
end, |
|||
nl |
|||
end, |
|||
nl. |
|||
% reversible variant (using CP) |
|||
multifactorial_reversible(N,_D,M) :- |
|||
N #<= 0, M #= 1. |
|||
multifactorial_reversible(N,D,M) :- |
|||
D #> 0, |
|||
N #> 0, |
|||
ND #= N-D, |
|||
multifactorial_reversible(ND,D,M1), |
|||
M #= N*M1.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Reversible: find Degree and N given M: |
|||
m = 4 |
|||
n=4 degree=3 m=4 |
|||
n=4 degree=4 m=4 |
|||
n=4 degree=5 m=4 |
|||
n=4 degree=6 m=4 |
|||
n=4 degree=7 m=4 |
|||
n=4 degree=8 m=4 |
|||
n=4 degree=9 m=4 |
|||
n=4 degree=10 m=4 |
|||
m = 20 |
|||
n=10 degree=8 m=20 |
|||
m = 105 |
|||
n=7 degree=2 m=105 |
|||
n=15 degree=8 m=105</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="picolisp">(de multifact (N Deg) |
||
(let Res N |
(let Res N |
||
(while (> N Deg) |
(while (> N Deg) |
||
Line 1,540: | Line 1,952: | ||
(for J 10 |
(for J 10 |
||
(prin " " (multifact J I)) ) |
(prin " " (multifact J I)) ) |
||
(prinl) )</ |
(prinl) )</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,549: | Line 1,961: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
multi: procedure options (main); /* 29 October 2013 */ |
multi: procedure options (main); /* 29 October 2013 */ |
||
declare (i, j, n) fixed binary; |
declare (i, j, n) fixed binary; |
||
Line 1,571: | Line 1,983: | ||
end multi; |
end multi; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,584: | Line 1,996: | ||
Works with an etex engine. |
Works with an etex engine. |
||
< |
<syntaxhighlight lang="tex">\long\def\antefi#1#2\fi{#2\fi#1} |
||
\def\fornum#1=#2to#3(#4){% |
\def\fornum#1=#2to#3(#4){% |
||
\edef#1{\number\numexpr#2}\edef\fornumtemp{\noexpand\fornumi\expandafter\noexpand\csname fornum\string#1\endcsname |
\edef#1{\number\numexpr#2}\edef\fornumtemp{\noexpand\fornumi\expandafter\noexpand\csname fornum\string#1\endcsname |
||
Line 1,597: | Line 2,009: | ||
} |
} |
||
\fornum\degree=1 to 5(+1){Degree \degree: \fornum\ii=1 to 10(+1){\multifact\ii\degree\space\space}\par} |
\fornum\degree=1 to 5(+1){Degree \degree: \fornum\ii=1 to 10(+1){\multifact\ii\degree\space\space}\par} |
||
\bye</ |
\bye</syntaxhighlight> |
||
Output pdf looks like: |
Output pdf looks like: |
||
Line 1,608: | Line 2,020: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python: Iterative=== |
===Python: Iterative=== |
||
< |
<syntaxhighlight lang="python">>>> from functools import reduce |
||
>>> from operator import mul |
>>> from operator import mul |
||
>>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) |
>>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) |
||
Line 1,624: | Line 2,036: | ||
9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
||
10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
||
>>> </ |
>>> </syntaxhighlight> |
||
===Python: Recursive=== |
===Python: Recursive=== |
||
< |
<syntaxhighlight 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)])) |
>>> for m in range(1, 6): print("%2i: %r" % (m, [mfac2(n, m) for n in range(1, 11)])) |
||
Line 1,636: | Line 2,048: | ||
4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] |
4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] |
||
5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] |
5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] |
||
>>> </ |
>>> </syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ 1 rot times |
||
[ i 1+ * |
[ i 1+ * |
||
dip [ over step ] ] |
dip [ over step ] ] |
||
Line 1,649: | Line 2,061: | ||
[ i^ 1+ over m! |
[ i^ 1+ over m! |
||
echo sp ] |
echo sp ] |
||
drop cr ]</ |
drop cr ]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,661: | Line 2,073: | ||
=={{header|R}}== |
=={{header|R}}== |
||
===Recursive solution=== |
===Recursive solution=== |
||
< |
<syntaxhighlight lang="rsplus">#x is Input |
||
#n is Factorial Number |
#n is Factorial Number |
||
multifactorial=function(x,n){ |
multifactorial=function(x,n){ |
||
Line 1,669: | Line 2,081: | ||
return(x*multifactorial(x-n,n)) |
return(x*multifactorial(x-n,n)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===Sequence solution=== |
===Sequence solution=== |
||
This task doesn't use big enough numbers to need efficient code, so R can solve this very succinctly. |
This task doesn't use big enough numbers to need efficient code, so R can solve this very succinctly. |
||
< |
<syntaxhighlight lang="rsplus">mFact <- function(n, deg) prod(seq(from = n, to = 1, by = -deg)) |
||
cat("Simple version:\n") |
|||
outer(1:10,1:5,Vectorize(mfact))</lang> |
|||
print(outer(1:10, 1:5, Vectorize(mFact)))</syntaxhighlight> |
|||
If we really insist on a pretty table, then we can add some names and transpose the output. |
If we really insist on a pretty table, then we can add some names and transpose the output. |
||
< |
<syntaxhighlight lang="rsplus">mFact <- function(n, deg) prod(seq(from = n, to = 1, by = -deg)) |
||
cat("Pretty version:\n") |
|||
n<-1:10; names(n)<-n |
|||
print(t(outer(setNames(1:10, 1:10), setNames(1:5, paste0("Degree ", 1:5, ":")), Vectorize(mFact))))</syntaxhighlight> |
|||
deg<-1:5; names(deg)<-paste("Degree",deg) |
|||
t(outer(n,deg,Vectorize(mfact)))</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Simple version: |
|||
<pre>> outer(1:10,1:5,Vectorize(mfact)) |
|||
[,1] [,2] [,3] [,4] [,5] |
[,1] [,2] [,3] [,4] [,5] |
||
[1,] 1 1 1 1 1 |
[1,] 1 1 1 1 1 |
||
Line 1,692: | Line 2,104: | ||
[9,] 362880 945 162 45 36 |
[9,] 362880 945 162 45 36 |
||
[10,] 3628800 3840 280 120 50 |
[10,] 3628800 3840 280 120 50 |
||
Pretty version: |
|||
1 2 3 4 5 6 7 8 9 10 |
|||
#"Pretty" version: |
|||
Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
|||
> t(outer(n,deg,Vectorize(mfact))) |
|||
Degree 2: 1 2 3 8 15 48 105 384 945 3840 |
|||
Degree |
Degree 3: 1 2 3 4 10 18 28 80 162 280 |
||
Degree |
Degree 4: 1 2 3 4 5 12 21 32 45 120 |
||
Degree |
Degree 5: 1 2 3 4 5 6 14 24 36 50</pre> |
||
Degree 4 1 2 3 4 5 12 21 32 45 120 |
|||
Degree 5 1 2 3 4 5 6 14 24 36 50</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (multi-factorial-fn m) |
(define (multi-factorial-fn m) |
||
Line 1,720: | Line 2,130: | ||
(for/list ([m (in-range 1 (add1 5))]) |
(for/list ([m (in-range 1 (add1 5))]) |
||
(for/list ([n (in-range 1 (add1 10))]) |
(for/list ([n (in-range 1 (add1 10))]) |
||
(multi-factorial m n)))</ |
(multi-factorial m n)))</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>'((1 2 6 24 120 720 5040 40320 362880 3628800) |
<pre>'((1 2 6 24 120 720 5040 40320 362880 3628800) |
||
Line 1,735: | Line 2,145: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>for 1 .. 5 -> $degree { |
||
sub mfact($n) { [*] $n, *-$degree ...^ * <= 0 }; |
sub mfact($n) { [*] $n, *-$degree ...^ * <= 0 }; |
||
say "$degree: ", map &mfact, 1..10 |
say "$degree: ", map &mfact, 1..10 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,748: | Line 2,158: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This version also handles zero as well as positive integers. |
This version also handles zero as well as positive integers. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program calculates and displays K-fact (multifactorial) of non-negative integers.*/ |
||
numeric digits 1000 /*get ka-razy with the decimal digits. */ |
numeric digits 1000 /*get ka-razy with the decimal digits. */ |
||
parse arg num deg . /*get optional arguments from the C.L. */ |
parse arg num deg . /*get optional arguments from the C.L. */ |
||
Line 1,765: | Line 2,175: | ||
exit /*stick a fork in it, we're all done. */ |
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 !</ |
Kfact: procedure; !=1; do j=arg(1) to 2 by -word(arg(2) 1,1); !=!*j; end; return !</syntaxhighlight> |
||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 1,783: | Line 2,193: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
see "Degree " + "|" + " Multifactorials 1 to 10" + nl |
see "Degree " + "|" + " Multifactorials 1 to 10" + nl |
||
see copy("-", 52) + nl |
see copy("-", 52) + nl |
||
Line 1,800: | Line 2,210: | ||
next |
next |
||
return fact |
return fact |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,810: | Line 2,220: | ||
4 | 1 2 3 4 5 12 21 32 45 120 |
4 | 1 2 3 4 5 12 21 32 45 120 |
||
5 | 1 2 3 4 5 6 14 24 36 50 |
5 | 1 2 3 4 5 6 14 24 36 50 |
||
</pre> |
|||
=={{header|RPL}}== |
|||
Recursivity is the simplest way to implement the task in RPL. |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
===Recursive=== |
|||
≪ IF DUP2 > THEN DUP2 - SWAP NFACT * ELSE DROP END ≫ |
|||
'NFACT' STO |
|||
===Iterative=== |
|||
≪ OVER |
|||
WHILE DUP2 < REPEAT OVER - DUP 4 ROLL * ROT ROT END |
|||
DROP2 |
|||
≫ |
|||
'NFACT' STO |
|||
≪ 1 5 FOR p |
|||
{ } 1 10 FOR n |
|||
n p NFACT + |
|||
NEXT NEXT |
|||
≫ EVAL |
|||
{{out}} |
|||
<pre> |
|||
5: { 1 2 6 24 120 720 5040 40320 362880 3628800 } |
|||
4: { 1 2 3 8 15 48 105 384 945 3840 } |
|||
3: { 1 2 3 4 10 18 28 80 162 280 } |
|||
2: { 1 2 3 4 5 12 21 32 45 120 } |
|||
1: { 1 2 3 4 5 6 14 24 36 50 } |
|||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def multifact(n, d) |
||
n.step(1, -d).inject( :* ) |
n.step(1, -d).inject( :* ) |
||
end |
end |
||
(1..5).each {|d| puts "Degree #{d}: #{(1..10).map{|n| multifact(n, d)}.join "\t"}"}</ |
(1..5).each {|d| puts "Degree #{d}: #{(1..10).map{|n| multifact(n, d)}.join "\t"}"}</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |
||
Line 1,827: | Line 2,264: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic"> |
||
print "Degree " + "|" + " Multifactorials 1 to 10" + nl |
print "Degree " + "|" + " Multifactorials 1 to 10" + nl |
||
print copy("-", 52) + nl |
print copy("-", 52) + nl |
||
Line 1,844: | Line 2,281: | ||
next |
next |
||
multiFact = fact |
multiFact = fact |
||
end function</ |
end function</syntaxhighlight> |
||
<pre>Degree | Multifactorials 1 to 10 |
<pre>Degree | Multifactorials 1 to 10 |
||
--------|--------------------------------------------- |
--------|--------------------------------------------- |
||
Line 1,855: | Line 2,292: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn multifactorial(n: i32, deg: i32) -> i32 { |
||
if n < 1 { |
if n < 1 { |
||
1 |
1 |
||
Line 1,870: | Line 2,307: | ||
println!(""); |
println!(""); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
1 2 6 24 120 720 5040 40320 362880 3628800 |
1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 1,880: | Line 2,317: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala"> |
||
def multiFact(n : BigInt, degree : BigInt) = (n to 1 by -degree).product |
def multiFact(n : BigInt, degree : BigInt) = (n to 1 by -degree).product |
||
Line 1,887: | Line 2,324: | ||
str = (1 to 10).map(n => multiFact(n, degree)).mkString(" ") |
str = (1 to 10).map(n => multiFact(n, degree)).mkString(" ") |
||
} println(s"Degree $degree: $str") |
} println(s"Degree $degree: $str") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,898: | Line 2,335: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(import (scheme base) |
(import (scheme base) |
||
(scheme write) |
(scheme write) |
||
Line 1,918: | Line 2,355: | ||
(newline)) |
(newline)) |
||
(iota 5 1)) |
(iota 5 1)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,930: | Line 2,367: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func integer: multiFact (in var integer: num, in integer: degree) is func |
const func integer: multiFact (in var integer: num, in integer: degree) is func |
||
Line 1,954: | Line 2,391: | ||
writeln; |
writeln; |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,964: | Line 2,401: | ||
Degree 5: 1 2 3 4 5 6 14 24 36 50 |
Degree 5: 1 2 3 4 5 6 14 24 36 50 |
||
</pre> |
</pre> |
||
=={{header|SETL}}== |
|||
<syntaxhighlight lang="setl">program multifactorial; |
|||
loop for d in [1..5] do |
|||
print(d, ":", [multifac(n, d) : n in [1..10]]); |
|||
end loop; |
|||
proc multifac(n, d); |
|||
return */{n, (n-d)..1}; |
|||
end proc; |
|||
end program;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>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]</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func mfact(s, n) { |
||
n > 0 ? (n * mfact(s, n-s)) : 1 |
n > 0 ? (n * mfact(s, n-s)) : 1 |
||
} |
} |
||
Line 1,972: | Line 2,426: | ||
{ |s| |
{ |s| |
||
say "step=#{s}: #{{|n| mfact(s, n)}.map(1..10).join(' ')}" |
say "step=#{s}: #{{|n| mfact(s, n)}.map(1..10).join(' ')}" |
||
} << 1..10</ |
} << 1..10</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,989: | Line 2,443: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func multiFactorial(_ n: Int, k: Int) -> Int { |
||
return stride(from: n, to: 0, by: -k).reduce(1, *) |
return stride(from: n, to: 0, by: -k).reduce(1, *) |
||
} |
} |
||
Line 2,001: | Line 2,455: | ||
for (i, degree) in multis.enumerated() { |
for (i, degree) in multis.enumerated() { |
||
print("Degree \(i + 1): \(degree)") |
print("Degree \(i + 1): \(degree)") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,012: | Line 2,466: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.6}} |
{{works with|Tcl|8.6}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.6 |
||
proc mfact {n m} { |
proc mfact {n m} { |
||
Line 2,022: | Line 2,476: | ||
foreach n {1 2 3 4 5 6 7 8 9 10} { |
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}] ,] |
puts $n:[join [lmap m {1 2 3 4 5 6 7 8 9 10} {mfact $m $n}] ,] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,039: | Line 2,493: | ||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
{{Trans|Run BASIC}} |
{{Trans|Run BASIC}} |
||
<lang>print "Degree | Multifactorials 1 to 10" |
<syntaxhighlight lang="text">print "Degree | Multifactorials 1 to 10" |
||
for x = 1 to 53 : print "-"; : next : print |
for x = 1 to 53 : print "-"; : next : print |
||
for d = 1 to 5 |
for d = 1 to 5 |
||
Line 2,057: | Line 2,511: | ||
c@ = c@ * d@ |
c@ = c@ * d@ |
||
next |
next |
||
return (c@)</ |
return (c@)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Degree | Multifactorials 1 to 10 |
<pre>Degree | Multifactorials 1 to 10 |
||
Line 2,070: | Line 2,524: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Function multifactorial(n,d) |
Function multifactorial(n,d) |
||
If n = 0 Then |
If n = 0 Then |
||
Line 2,096: | Line 2,550: | ||
WScript.StdOut.WriteLine |
WScript.StdOut.WriteLine |
||
Next |
Next |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 2,108: | Line 2,562: | ||
=={{header|Wortel}}== |
=={{header|Wortel}}== |
||
< |
<syntaxhighlight lang="wortel">@let { |
||
facd &[d n]?{<= n d n @prod@range[n 1 @-d]} |
facd &[d n]?{<= n d n @prod@range[n 1 @-d]} |
||
; tacit implementation |
; tacit implementation |
||
Line 2,117: | Line 2,571: | ||
l @to 10 |
l @to 10 |
||
~@each @to 5 &n !console.log "Degree {n}: {@join @s !*\facd n l}" |
~@each @to 5 &n !console.log "Degree {n}: {@join @s !*\facd n l}" |
||
}</ |
}</syntaxhighlight> |
||
Output |
Output |
||
<pre>Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
<pre>Degree 1: 1 2 6 24 120 720 5040 40320 362880 3628800 |
||
Line 2,127: | Line 2,581: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="wren">import "./fmt" for Fmt |
||
var mf = Fn.new { |n, d| |
var mf = Fn.new { |n, d| |
||
Line 2,142: | Line 2,596: | ||
for (n in 1..10) System.write(Fmt.d(8, mf.call(n, d))) |
for (n in 1..10) System.write(Fmt.d(8, mf.call(n, d))) |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,154: | Line 2,608: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code ChOut=8, CrLf=9, IntOut=11; |
||
func MultiFac(N, D); \Return multifactorial of N in degree D |
func MultiFac(N, D); \Return multifactorial of N in degree D |
||
Line 2,171: | Line 2,625: | ||
[IntOut(0, MultiFac(I, J)); ChOut(0, 9\tab\)]; |
[IntOut(0, MultiFac(I, J)); ChOut(0, 9\tab\)]; |
||
CrLf(0); |
CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,183: | Line 2,637: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight 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)))) }</ |
foreach m in ([1..5]){ println("%d: %s".fmt(m,[1..10].apply(mfact.fp1(m)))) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 14:12, 4 January 2024
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:
- 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 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.
11l
F multifact(n, d)
R product((n .< 1).step(-d))
L(d) 1..5
print(‘Degree ’d‘: ’(1..10).map(n -> multifact(n, @d)))
- 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]
F multifact(=n, d)
V prod = 1
L n > 1
prod *= n
n -= d
R prod
360 Assembly
For maximum compatibility, this program uses only the basic instruction set (S/360 1964 POP).
* Multifactorial 09/05/2016
MULFACR CSECT
USING MULFACR,13
SAVEAR B STM-SAVEAR(15)
DC 17F'0'
STM STM 14,12,12(13) prolog
ST 13,4(15) "
ST 15,8(13) "
LR 13,15 "
LA I,1 i=1
LOOPI C I,D do i=1 to deg
BH ELOOPI leave i
LA L,W+4 l=@p
LA J,1 j=1
LOOPJ C J,N do j=1 to num
BH ELOOPJ leave j
LA R,1 r=1
LCR S,I s=-i
LR K,J k=j
LOOPK C K,=F'2' do k=j to 2 by s
BL ELOOPK leave k
MR RR,K r=r*k
AR K,S k=k+s
B LOOPK next k
ELOOPK CVD R,Y pack r
MVC X,=XL12'402020202020202020202120' ed mask
ED X,Y+2 edit r
MVC 0(8,L),X+4 output r
LA L,8(L) l=l+8
LA J,1(J) j=j+1
B LOOPJ next j
ELOOPJ WTO MF=(E,W)
LA I,1(I) i=i+1
B LOOPI next i
ELOOPI L 13,4(0,13) epilog
LM 14,12,12(13) "
XR 15,15 "
BR 14 "
N DC F'10' number
D DC F'5' degree
W DC 0F,H'84',H'0',CL80' ' length,zero,text
X DS CL12 temp
Y DS D packed PL8
I EQU 6
J EQU 7
K EQU 8
S EQU 9
RR EQU 10 even reg of R for MR opcode
R EQU 11
L EQU 12
END MULFACR
- 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
Action!
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
PROC Multifactorial(INT n,d REAL POINTER res)
REAL r
IntToReal(1,res)
WHILE n>1
DO
IntToReal(n,r)
RealMult(res,r,res)
n==-d
OD
RETURN
PROC Main()
BYTE n,d
REAL r
Put(125) PutE() ;clear the screen
FOR d=1 TO 5
DO
PrintF("Degree %B:",d)
FOR n=1 TO 10
DO
Multifactorial(n,d,r)
Put(32) PrintR(r)
OD
PutE()
OD
RETURN
- Output:
Screenshot from Atari 8-bit computer
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
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;
- 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
Aime
mf(integer a, n)
{
integer o;
o = 1;
do {
o *= a;
} while (0 < (a -= n));
o;
}
main(void)
{
integer i, j;
i = 0;
while ((i += 1) <= 5) {
o_("degree ", i, ":");
j = 0;
while ((j += 1) <= 10) {
o_("\t", mf(j, i));
}
o_("\n");
}
0;
}
- 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.
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
- 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 W
Iterative multifactorial based on Ada, AutoHotkey, etc.
begin
% returns the multifactorial of n with the specified degree %
integer procedure multifactorial ( integer value n, degree ) ;
begin
integer mf, v;
mf := v := n;
while begin
v := v - degree;
v > 1
end do mf := mf * v;
mf
end multifactorial ;
% tests as per task %
for degree := 1 until 5 do begin
i_w := 1; s_w := 0; % output formatting %
write( "Degree: ", degree, ":" );
for v := 1 until 10 do begin
writeon( " ", multifactorial( v, degree ) )
end for_v
end for_degree
end.
- 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
AppleScript
on multifactorial(n, d)
set f to 1
repeat with n from n to 2 by -d
set f to f * n
end repeat
return f
end multifactorial
on task()
set table to ""
repeat with degree from 1 to 5
set row to linefeed & "Degree " & degree & ":"
repeat with n from 1 to 10
set row to row & (space & multifactorial(n, degree))
end repeat
set table to table & row
end repeat
return table
end task
task()
- 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"
Arturo
multifact: function [n deg][
if? n =< deg -> n
else -> n * multifact n-deg deg
]
loop 1..5 'i [
prints ["Degree" i ":"]
loop 1..10 'j [
prints [multifact j i " "]
]
print ""
]
- 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
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
}
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
AWK
# syntax: GAWK -f MULTIFACTORIAL.AWK
# converted from Go
BEGIN {
for (k=1; k<=5; k++) {
printf("degree %d:",k)
for (n=1; n<=10; n++) {
printf(" %d",multi_factorial(n,k))
}
printf("\n")
}
exit(0)
}
function multi_factorial(n,k, r) {
r = 1
for (; n>1; n-=k) {
r *= n
}
return(r)
}
- 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
BASIC
ANSI BASIC
100 FUNCTION multiFactorial (n, degree)
110 IF n < 2 THEN
120 LET multiFactorial = 1
130 EXIT FUNCTION
140 END IF
150 LET result = n
160 FOR i = n - degree TO 2 STEP -degree
170 LET result = result * i
180 NEXT i
190 LET multiFactorial = result
200 END FUNCTION
210
220 FOR degree = 1 TO 5
230 PRINT "Degree"; degree; " => ";
240 FOR n = 1 TO 10
250 PRINT multiFactorial(n, degree); " ";
260 NEXT n
270 PRINT
280 NEXT degree
290 END
- 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
BBC BASIC
REM >multifact
FOR i% = 1 TO 5
PRINT "Degree "; i%; ":";
FOR j% = 1 TO 10
PRINT " ";FNmultifact(j%, i%);
NEXT
PRINT
NEXT
END
:
DEF FNmultifact(n%, degree%)
LOCAL i%, mfact%
mfact% = 1
FOR i% = n% TO 1 STEP -degree%
mfact% = mfact% * i%
NEXT
= mfact%
- 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
IS-BASIC
100 PROGRAM "MultiFac.bas"
110 FOR I=1 TO 5
120 PRINT "Degree";I;": ";
130 FOR J=1 TO 10
140 PRINT MFACT(J,I);" ";
150 NEXT
160 PRINT
170 NEXT
180 DEF MFACT(N,DEGREE)
190 LET RESULT=1
200 FOR X=N TO 1 STEP-DEGREE
210 LET RESULT=RESULT*X
220 NEXT
230 LET MFACT=RESULT
240 END DEF
BQN
MultiFact ← ×´⊣-↕∘⌈∘÷×⊢
MultiFact⌜⟜(5⊸↑) 1+↕10
- Output:
┌─ ╵ 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 ┘
C
/* Include statements and constant definitions */
#include <stdio.h>
#define HIGHEST_DEGREE 5
#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));
}
}
}
- 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#
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));
}
}
}
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++
#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
Clojure
(defn !! [m n]
(->> (iterate #(- % m) n) (take-while pos?) (apply *)))
(doseq [m (range 1 6)]
(prn m (map #(!! m %) (range 1 11))))
- 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)
CLU
multifactorial = proc (n, degree: int) returns (int)
result: int := 1
for i: int in int$from_to_by(n, 1, -degree) do
result := result * i
end
return (result)
end multifactorial
start_up = proc ()
po: stream := stream$primary_output()
for n: int in int$from_to(1, 10) do
for d: int in int$from_to(1, 5) do
stream$putright(po, int$unparse(multifactorial(n,d)), 10)
end
stream$putc(po, '\n')
end
end start_up
- Output:
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
Common 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))))
- 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
Cowgol
include "cowgol.coh";
sub multifac(n: uint32, d: uint32): (r: uint32) is
r := 1;
loop
r := r * n;
if n <= d then break; end if;
n := n - d;
end loop;
end sub;
var d: uint32 := 1;
while d <= 5 loop
print_i32(d);
print(": ");
var n: uint32 := 1;
while n <= 10 loop
print_i32(multifac(n, d));
print(" ");
n := n + 1;
end loop;
print_nl();
d := d + 1;
end loop;
- 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
Crystal
def multifact(n, d)
n.step(to: 1, by: -d).product
end
(1..5).each {|d| puts "Degree #{d}: #{(1..10).map{|n| multifact(n, d)}.join "\t"}"}
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
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)));
}
- 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
Dart
main()
{
int n=5,d=3;
int z= fact(n,d);
print('$n factorial of degree $d is $z');
for(var j=1;j<=5;j++)
{
print('first 10 numbers of degree $j :');
for(var i=1;i<=10;i++)
{
int z=fact(i,j);
print('$z');
}
print('\n');
}
}
int fact(int a,int b)
{
if(a<=b||a==0)
return a;
if(a>1)
return a*fact((a-b),b);
}
Draco
proc nonrec multifac(int n, deg) ulong:
ulong result;
result := 1;
while n > 1 do
result := result * n;
n := n - deg
od;
result
corp
proc nonrec main() void:
byte n, d;
for n from 1 upto 10 do
for d from 1 upto 5 do
write(multifac(n,d):10)
od;
writeln()
od
corp
- Output:
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
Delphi
function MultiFact(Num,Deg: integer): integer;
{Multifactorial from Degree and Number}
var N: integer;
begin
N:=Num;
Result:=Num;
if N = 0 then Result:=1
else while true do
begin
N := N - deg;
if N<1 then break;
Result:=Result * N;
end;
end;
procedure ShowMultifactorial(Memo: TMemo);
{Show combinations of deg/num of multifactorial}
var Deg,Num: integer;
var S: string;
begin
S:='';
for Deg:=1 to 5 do
begin
S:=S+Format('Degree: %d:',[Deg]);
for Num:=1 to 10 do S:=S+' '+Format('%7d',[MultiFact(Num,Deg)]);
S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
- 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
EasyLang
func mfact n k .
r = 1
while n > 1
r *= n
n -= k
.
return r
.
for k = 1 to 5
write "degree " & k & ":"
for n = 1 to 10
write " " & mfact n k
.
print ""
.
Elixir
defmodule RC do
def multifactorial(n,d) do
Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end)
end
end
Enum.each(1..5, fn d ->
multifac = for n <- 1..10, do: RC.multifactorial(n,d)
IO.puts "Degree #{d}: #{inspect multifac}"
end)
- 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
-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).
- Output:
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
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
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#
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
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]
Factor
USING: formatting io kernel math math.ranges prettyprint
sequences ;
IN: rosetta-code.multifactorial
: multifactorial ( n degree -- m )
neg 1 swap <range> product ;
: mf-row ( degree -- )
dup "Degree %d: " printf
10 [1,b] [ swap multifactorial pprint bl ] with each ;
: main ( -- )
5 [1,b] [ mf-row nl ] each ;
MAIN: main
- 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
Forth
: !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 ;
- 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
Fortran
program test
implicit none
integer :: i, j, n
do i = 1, 5
write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": "
do j = 1, 10
n = multifactorial(j, i)
write(*, "(i0, 1x)", advance = "no") n
end do
write(*,*)
end do
contains
function multifactorial (range, degree)
integer :: multifactorial, range, degree
integer :: k
multifactorial = product((/(k, k=range, 1, -degree)/))
end function multifactorial
end program test
- 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
FreeBASIC
' FB 1.05.0 Win64
Function multiFactorial (n As UInteger, degree As Integer) As UInteger
If n < 2 Then Return 1
Var result = n
For i As Integer = n - degree To 2 Step -degree
result *= i
Next
Return result
End Function
For degree As Integer = 1 To 5
Print "Degree"; degree; " => ";
For n As Integer = 1 To 10
Print multiFactorial(n, degree); " ";
Next n
Print
Next degree
Print
Print "Press any key to quit"
Sleep
- 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
FunL
def multifactorial( n, d ) = product( n..1 by -d )
for d <- 1..5
println( d, [multifactorial(i, d) | i <- 1..10] ))
- 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
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 ] ]
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()
}
}
- 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
mulfac :: (Num a, Enum a) => a -> [a]
mulfac k = 1 : s
where
s = [1 .. k] <> zipWith (*) s [k + 1 ..]
-- For single n:
mulfac1 :: (Num a, Enum a) => a -> a -> a
mulfac1 k n = product [n, n - k .. 1]
main :: IO ()
main =
mapM_
(print . take 10 . tail . mulfac)
[1 .. 5]
- 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:
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
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 ->
IS-BASIC
100 PROGRAM "Multifac.bas"
110 FOR I=1 TO 5
120 PRINT "Degree";I;":";
130 FOR N=1 TO 10
140 PRINT MFACT(N,I);
150 NEXT
160 PRINT
170 NEXT
180 DEF MFACT(N,D)
190 NUMERIC I,RES
200 IF N<2 THEN LET MFACT=1:EXIT DEF
210 LET RES=N
220 FOR I=N-D TO 2 STEP-D
230 LET RES=RES*I
240 NEXT
250 LET MFACT=RES
260 END DEF
J
NB. n multifact degree
multifact=: */@([ - ] * i.@>.@%)&>
('';' 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│
└─────────┴──────────────────────────────────────┘
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();
}
}
}
- 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
function multifact(n, deg){
var result = n;
while (n >= deg + 1){
result *= (n - deg);
n -= deg;
}
return result;
}
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);
}
}
- Output:
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
Recursive
function multifact(n, deg){
return n <= deg ? n : n * multifact(n - deg, deg);
}
Test
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);
}
}
- Output:
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
jq
# Input: n
# 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);
# 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)) )";
The specific task:
table(5; 10)
- Output:
$ 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
Julia
using Printf
function multifact(n::Integer, k::Integer)
n > 0 && k > 0 || throw(DomainError())
k > 1 || factorial(n)
return prod(n:-k:2)
end
const khi = 5
const nhi = 10
println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].")
for k = 1:khi
a = multifact.(1:nhi, k)
lab = "n" * "!" ^ k
@printf(" %-6s → %s\n", lab, a)
end
- 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]
Kotlin
fun multifactorial(n: Long, d: Int) : Long {
val r = n % d
return (1..n).filter { it % d == r } .reduce { i, p -> i * p }
}
fun main(args: Array<String>) {
val m = 5
val r = 1..10L
for (d in 1..m) {
print("%${m}s:".format( "!".repeat(d)))
r.forEach { print(" " + multifactorial(it, d)) }
println()
}
}
- 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
Lambdatalk
{def multifact
{lambda {:n :deg}
{if {<= :n :deg}
then :n
else {* :n {multifact {- :n :deg} :deg}}}}}
-> multifact
{S.map {lambda {:deg} {br} Degree :deg:
{S.map {{lambda {:deg :n} {multifact :n :deg}} :deg}
{S.serie 1 10}}}
{S.serie 1 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
Latitude
use 'format importAllSigils.
multiFactorial := {
Range make ($1, 0, - $2) product.
}.
1 upto 6 visit {
takes '[degree].
answers := 1 upto 11 to (Array) map { multiFactorial ($1, degree). }.
$stdout printf: ~fmt "Degree ~S: ~S", degree, answers.
}.
- 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]
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
- 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
MAD
NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N,DEG)
ENTRY TO MLTFAC.
RSLT = 1
THROUGH MULT, FOR MPC=N, -DEG, MPC.L.1
MULT RSLT = RSLT * MPC
FUNCTION RETURN RSLT
END OF FUNCTION
THROUGH SHOW, FOR I=1, 1, I.G.10
SHOW PRINT FORMAT OUTP, MLTFAC.(I,1), MLTFAC.(I,2),
0 MLTFAC.(I,3), MLTFAC.(I,4), MLTFAC.(I,5)
VECTOR VALUES OUTP = $5(I10,S1)*$
END OF PROGRAM
- Output:
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
Maple
f := proc (n, m)
local fac, i;
fac := 1;
for i from n by -m to 1 do
fac := fac*i;
end do;
return fac;
end proc:
a:=Matrix(5,10):
for i from 1 to 5 do
for j from 1 to 10 do
a[i,j]:=f(j,i);
end do;
end do;
a;
- 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]
Mathematica/Wolfram Language
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}]
Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
- 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
Maxima
Using built-in function genfact
multifactorial(x,n):=genfact(x,x/n,n)$
/* Test case */
makelist(multifactorial(i,1),i,1,10);
makelist(multifactorial(i,2),i,1,10);
block(makelist(mod(i,3),i,1,10),at(%%,0=1),%%*makelist(multifactorial(i,3),i,1,10));
block(makelist(mod(i,4),i,1,10),at(%%,0=1),%%*makelist(multifactorial(i,4),i,1,10));
block(makelist(mod(i,5),i,1,10),at(%%,0=1),%%*makelist(multifactorial(i,5),i,1,10));
- 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]
min
(:d (dup 0 <=) (pop 1) (dup d -) (*) linrec) :multifactorial
(:d 1 (dup d multifactorial print! " " print! succ) 10 times newline pop) :row
1 (dup "Degree " print! print ": " print! row succ) 5 times
- 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
Miranda
main :: [sys_message]
main = [ Stdout (show deg ++ ": " ++ show (map (multifac deg) [1..10]) ++ "\n")
| deg <- [1..5]]
multifac :: num->num->num
multifac deg = product . takewhile (>1) . iterate sub
where sub n = n - deg
- 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
П1 <-> П0 П2 ИП0 ИП1 1 + - x>=0
23 ИП2 ИП0 ИП1 - * П2 ИП0 ИП1 -
П1 БП 04 ИП2 С/П
Instruction: number ^ degree В/О С/П
Nim
# Recursive
proc multifact(n, deg: int): int =
result = (if n <= deg: n else: n * multifact(n - deg, deg))
# Iterative
proc multifactI(n, deg: int): int =
result = n
var n = n
while n >= deg + 1:
result *= n - deg
n -= deg
for i in 1..5:
stdout.write "Degree ", i, ": "
for j in 1..10:
stdout.write multifactI(j, i), " "
stdout.write('\n')
- 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
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();
};
}
}
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
OCaml
let multi_fac d n =
let rec loop a x = if x < 2 then a else loop (a * x) (x - d) in
loop n (n - d)
let () =
for i = 1 to 5 do
Seq.(ints 1 |> take 10 |> map (multi_fac i) |> map string_of_int)
|> List.of_seq |> String.concat " " |> print_endline
done
- 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
Oforth
: multifact(n, deg) 1 while( n 0 > ) [ n * n deg - ->n ] ;
: printMulti
| i |
5 loop: i [ System.Out i << " : " << 10 seq map(#[ i multifact]) << cr ] ;
- 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]
Ol
(define (multifactorial n d)
(fold * 1 (iota (div n d) n (negate d))))
(for-each (lambda (i)
(display "Degree ")
(display i)
(display ":")
(for-each (lambda (n)
(display " ")
(display (multifactorial n i)))
(iota 10 1))
(print))
(iota 5 1))
- 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 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
By the way, we can create few multifactorial functions and use them directly or as part of infix math notation (inside "//" macro).
(define (!!!!! n) (multifactorial n 5))
(print (!!!!! 74))
(import (math infix-notation))
; register !!!!! as a postfix function
(define \\postfix-functions (put \\postfix-functions '!!!!! #t))
; now use "\\" as usual
(print (\\
2 + 74!!!!!
))
- Output:
4959435223298761261056 4959435223298761261058
PARI/GP
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)
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
{ # <-- 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";
}
- 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
We can also do this iteratively. ntheory's vecprod makes bigint products if needed, so we don't have to worry about it.
use ntheory qw/vecprod/;
sub mfac {
my($n,$d) = @_;
vecprod(map { $n - $_*$d } 0 .. int(($n-1)/$d));
}
for my $degree (1..5) {
say "$degree: ",join(" ",map{mfac($_,$degree)} 1..10);
}
- 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
Phix
with javascript_semantics function multifactorial(integer n, order) atom res = 1 if n>0 then res = n*multifactorial(n-order,order) end if return res end function sequence s = repeat(0,10) for i=1 to 5 do for j=1 to 10 do s[j] = multifactorial(j,i) end for pp(s) end for
- 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}
Picat
Using prod/1
multifactorial(N,Degree) = prod([ I : I in N..-Degree..1]).
Using reduce/2
multifactorial2(N,Degree) = reduce(*, [I : I in N..-Degree..1]).
While loop
multifactorial3(N,Degree) = M =>
M = 1, I = N,
while(I > 0)
M := M*I,
I := I - Degree
end.
Recursive variants
multifactorial4(N,_D) = 1, N <= 0 => true.
multifactorial4(N,D) = N*multifactorial4(N-D,D).
multifactorial5(N,D) = M =>
N <= 0 -> M = 1 ; M = N*multifactorial4(N-D,D).
multifactorial6(N,D) = cond(N <= 0, 1, N*multifactorial6(N-D,D)).
Test
import util.
go =>
foreach(D in 1..15)
println(D=[multifactorial(I,D) : I in 1..15])
end,
nl.
- Output:
1 = [1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000] 2 = [1,2,3,8,15,48,105,384,945,3840,10395,46080,135135,645120,2027025] 3 = [1,2,3,4,10,18,28,80,162,280,880,1944,3640,12320,29160] 4 = [1,2,3,4,5,12,21,32,45,120,231,384,585,1680,3465] 5 = [1,2,3,4,5,6,14,24,36,50,66,168,312,504,750] 6 = [1,2,3,4,5,6,7,16,27,40,55,72,91,224,405] 7 = [1,2,3,4,5,6,7,8,18,30,44,60,78,98,120] 8 = [1,2,3,4,5,6,7,8,9,20,33,48,65,84,105] 9 = [1,2,3,4,5,6,7,8,9,10,22,36,52,70,90] 10 = [1,2,3,4,5,6,7,8,9,10,11,24,39,56,75] 11 = [1,2,3,4,5,6,7,8,9,10,11,12,26,42,60] 12 = [1,2,3,4,5,6,7,8,9,10,11,12,13,28,45] 13 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,30] 14 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] 15 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
Constraint modelling
Using constraint modelling for a reversible variant (i.e. all parameters can be inputs or outputs); here shown by identifying all the valid N and the degree given the multifactorial (M).
import cp.
%
% Reversible: find Degree and N given M
%
go2 =>
Ms = [4,20,105], % The multifactorials to identify
foreach(M in Ms)
println(m=M),
Degree :: 1..10, % limit of the degree
N :: 1..100, % limit of N
All = findall([N,Degree,M], (multifactorial_reversible(N,Degree,M),
solve([M,N,Degree]))),
foreach([NN,DD,MM] in All.sort)
printf("n=%d degree=%d m=%d\n",NN,DD,MM)
end,
nl
end,
nl.
% reversible variant (using CP)
multifactorial_reversible(N,_D,M) :-
N #<= 0, M #= 1.
multifactorial_reversible(N,D,M) :-
D #> 0,
N #> 0,
ND #= N-D,
multifactorial_reversible(ND,D,M1),
M #= N*M1.
- Output:
Reversible: find Degree and N given M: m = 4 n=4 degree=3 m=4 n=4 degree=4 m=4 n=4 degree=5 m=4 n=4 degree=6 m=4 n=4 degree=7 m=4 n=4 degree=8 m=4 n=4 degree=9 m=4 n=4 degree=10 m=4 m = 20 n=10 degree=8 m=20 m = 105 n=7 degree=2 m=105 n=15 degree=8 m=105
PicoLisp
(de multifact (N Deg)
(let Res N
(while (> N Deg)
(setq Res (* Res (dec 'N Deg))) )
Res ) )
(for I 5
(prin "Degree " I ":")
(for J 10
(prin " " (multifact J I)) )
(prinl) )
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
PL/I
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;
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
Plain TeX
Works with an etex engine.
\long\def\antefi#1#2\fi{#2\fi#1}
\def\fornum#1=#2to#3(#4){%
\edef#1{\number\numexpr#2}\edef\fornumtemp{\noexpand\fornumi\expandafter\noexpand\csname fornum\string#1\endcsname
{\number\numexpr#3}{\ifnum\numexpr#4<0 <\else>\fi}{\number\numexpr#4}\noexpand#1}\fornumtemp
}
\long\def\fornumi#1#2#3#4#5#6{\def#1{\unless\ifnum#5#3#2\relax\antefi{#6\edef#5{\number\numexpr#5+(#4)\relax}#1}\fi}#1}
\newcount\result
\def\multifact#1#2{%
\result=1
\fornum\multifactiter=#1 to 1(-#2){\multiply\result\multifactiter}%
\number\result
}
\fornum\degree=1 to 5(+1){Degree \degree: \fornum\ii=1 to 10(+1){\multifact\ii\degree\space\space}\par}
\bye
Output pdf looks like:
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
Python
Python: Iterative
>>> 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]
>>>
Python: Recursive
>>> 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]
>>>
Quackery
[ 1 rot times
[ i 1+ *
dip [ over step ] ]
nip ] is m! ( n --> n! )
5 times
[ i^ 1+ 10 times
[ i^ 1+ over m!
echo sp ]
drop cr ]
- 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
R
Recursive solution
#x is Input
#n is Factorial Number
multifactorial=function(x,n){
if(x<=n+1){
return(x)
}else{
return(x*multifactorial(x-n,n))
}
}
Sequence solution
This task doesn't use big enough numbers to need efficient code, so R can solve this very succinctly.
mFact <- function(n, deg) prod(seq(from = n, to = 1, by = -deg))
cat("Simple version:\n")
print(outer(1:10, 1:5, Vectorize(mFact)))
If we really insist on a pretty table, then we can add some names and transpose the output.
mFact <- function(n, deg) prod(seq(from = n, to = 1, by = -deg))
cat("Pretty version:\n")
print(t(outer(setNames(1:10, 1:10), setNames(1:5, paste0("Degree ", 1:5, ":")), Vectorize(mFact))))
- Output:
Simple version: [,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 2 2 2 2 2 [3,] 6 3 3 3 3 [4,] 24 8 4 4 4 [5,] 120 15 10 5 5 [6,] 720 48 18 12 6 [7,] 5040 105 28 21 14 [8,] 40320 384 80 32 24 [9,] 362880 945 162 45 36 [10,] 3628800 3840 280 120 50 Pretty version: 1 2 3 4 5 6 7 8 9 10 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
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)))
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))
Raku
(formerly Perl 6)
for 1 .. 5 -> $degree {
sub mfact($n) { [*] $n, *-$degree ...^ * <= 0 };
say "$degree: ", map &mfact, 1..10
}
- 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
REXX
This version also handles zero as well as positive integers.
/*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 !
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
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
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
RPL
Recursivity is the simplest way to implement the task in RPL.
Recursive
≪ IF DUP2 > THEN DUP2 - SWAP NFACT * ELSE DROP END ≫ 'NFACT' STO
Iterative
≪ OVER WHILE DUP2 < REPEAT OVER - DUP 4 ROLL * ROT ROT END DROP2 ≫ 'NFACT' STO
≪ 1 5 FOR p { } 1 10 FOR n n p NFACT + NEXT NEXT ≫ EVAL
- Output:
5: { 1 2 6 24 120 720 5040 40320 362880 3628800 } 4: { 1 2 3 8 15 48 105 384 945 3840 } 3: { 1 2 3 4 10 18 28 80 162 280 } 2: { 1 2 3 4 5 12 21 32 45 120 } 1: { 1 2 3 4 5 6 14 24 36 50 }
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"}"}
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
Run BASIC
print "Degree " + "|" + " Multifactorials 1 to 10" + nl
print copy("-", 52) + nl
for d = 1 to 5
print "" + d + " " + "| "
for n = 1 to 10
print "" + multiFact(n, d) + " ";
next
print
next
function multiFact(n,degree)
fact = 1
for i = n to 2 step -degree
fact = fact * i
next
multiFact = fact
end function
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
Rust
fn multifactorial(n: i32, deg: i32) -> i32 {
if n < 1 {
1
} else {
n * multifactorial(n - deg, deg)
}
}
fn main() {
for i in 1..6 {
for j in 1..11 {
print!("{} ", multifactorial(j, i));
}
println!("");
}
}
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
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")
- 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
Scheme
(import (scheme base)
(scheme write)
(srfi 1))
(define (multi-factorial n m)
(fold * 1 (iota (ceiling (/ n m)) n (- m))))
(for-each
(lambda (degree)
(display (string-append "degree "
(number->string degree)
": "))
(for-each
(lambda (num)
(display (string-append (number->string (multi-factorial num degree))
" ")))
(iota 10 1))
(newline))
(iota 5 1))
- 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
$ 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;
- 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
SETL
program multifactorial;
loop for d in [1..5] do
print(d, ":", [multifac(n, d) : n in [1..10]]);
end loop;
proc multifac(n, d);
return */{n, (n-d)..1};
end proc;
end program;
- 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]
Sidef
func mfact(s, n) {
n > 0 ? (n * mfact(s, n-s)) : 1
}
{ |s|
say "step=#{s}: #{{|n| mfact(s, n)}.map(1..10).join(' ')}"
} << 1..10
- 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
Swift
func multiFactorial(_ n: Int, k: Int) -> Int {
return stride(from: n, to: 0, by: -k).reduce(1, *)
}
let multis = (1...5).map({degree in
(1...10).map({member in
multiFactorial(member, k: degree)
})
})
for (i, degree) in multis.enumerated() {
print("Degree \(i + 1): \(degree)")
}
- 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]
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}] ,]
}
- 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
uBasic/4tH
print "Degree | Multifactorials 1 to 10"
for x = 1 to 53 : print "-"; : next : print
for d = 1 to 5
print d;" ";"| ";
for n = 1 to 10
print FUNC(_multiFact(n, d));" ";
next
print
next
end
_multiFact param (2)
local (2)
c@ = 1
for d@ = a@ to 2 step -b@
c@ = c@ * d@
next
return (c@)
- 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 0 OK, 0:1063
VBScript
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
- 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
@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}"
}
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
Wren
import "./fmt" for Fmt
var mf = Fn.new { |n, d|
var prod = 1
while (n > 1) {
prod = prod * n
n = n - d
}
return prod
}
for (d in 1..5) {
System.write("degree %(d): ")
for (n in 1..10) System.write(Fmt.d(8, mf.call(n, d)))
System.print()
}
- 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
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);
]
- 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
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)))) }
- 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)
- Programming Tasks
- Solutions by Programming Task
- 11l
- 360 Assembly
- Action!
- Action! Tool Kit
- Ada
- Aime
- ALGOL 68
- ALGOL W
- AppleScript
- Arturo
- AutoHotkey
- AWK
- BASIC
- ANSI BASIC
- BBC BASIC
- IS-BASIC
- BQN
- C
- C Runtime
- C sharp
- C++
- Clojure
- CLU
- Common Lisp
- Cowgol
- Crystal
- D
- Dart
- Draco
- Delphi
- SysUtils,StdCtrls
- EasyLang
- Elixir
- Erlang
- ERRE
- F Sharp
- Factor
- Forth
- Fortran
- FreeBASIC
- FunL
- GAP
- Go
- Haskell
- Unicon
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Lambdatalk
- Latitude
- Lua
- MAD
- Maple
- Mathematica
- Wolfram Language
- Maxima
- Min
- Miranda
- МК-61/52
- Nim
- Objeck
- OCaml
- Oforth
- Ol
- PARI/GP
- Perl
- Ntheory
- Phix
- Picat
- PicoLisp
- PL/I
- PlainTeX
- Python
- Quackery
- R
- Racket
- Raku
- REXX
- Ring
- RPL
- Ruby
- Run BASIC
- Rust
- Scala
- Scheme
- Seed7
- SETL
- Sidef
- Swift
- Tcl
- UBasic/4tH
- VBScript
- Wortel
- Wren
- Wren-fmt
- XPL0
- Zkl