Multifactorial: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(14 intermediate revisions by 10 users not shown)
Line 284:
</pre>
 
=={{header|ANSI Standard BASICAppleScript}}==
<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>
<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
160Degree 3: 1 2 FOR3 i4 =10 n18 -28 degree TO 280 STEP162 -degree280
170Degree 4: 1 2 3 4 5 LET12 result21 =32 result45 * i120
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</syntaxhighlight>
 
=={{header|Arturo}}==
Line 383 ⟶ 389:
</pre>
 
=={{header|BBC BASIC}}==
==={{header|ANSI BASIC}}===
{{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
Line 407 ⟶ 446:
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|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}}==
Line 597 ⟶ 671:
9: 1 2 3 4 5 6 7 8 9 10
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}}==
Line 698 ⟶ 804:
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}}==
Line 1,380 ⟶ 1,554:
4: 1 2 3 4 5 12 21 32 45 120
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}}==
Line 1,395 ⟶ 1,590:
Degree 5: 1 2 3 4 5 6 14 24 36 50
</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}}==
Line 1,498 ⟶ 1,708:
4 : [1, 2, 3, 4, 5, 12, 21, 32, 45, 120]
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>
 
Line 1,970 ⟶ 2,225:
Recursivity is the simplest way to implement the task in RPL.
{{works with|Halcyon Calc|4.2.7}}
'''===Recursive version'''===
≪ IF DUP2 > THEN DUP2 - SWAP NFACT * ELSE DROP END ≫
'NFACT' STO
'''===Iterative version'''===
≪ OVER
WHILE DUP2 < REPEAT OVER - DUP 4 ROLL * ROT ROT END
Line 2,146 ⟶ 2,401:
Degree 5: 1 2 3 4 5 6 14 24 36 50
</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}}==
Line 2,309 ⟶ 2,581:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var mf = Fn.new { |n, d|
9,482

edits