Multifactorial: Difference between revisions

m
(→‎{{header|ANSI Standard BASIC}}: Changed to {{header|ANSI BASIC}}; {{works with|Decimal BASIC}}; output.)
m (→‎{{header|Wren}}: Minor tidy)
 
(9 intermediate revisions by 7 users not shown)
Line 284:
</pre>
 
=={{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()
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>
 
{{output}}
<syntaxhighlight lang="applescript">"
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"</syntaxhighlight>
 
=={{header|Arturo}}==
Line 416 ⟶ 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 606 ⟶ 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 755 ⟶ 852:
</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,437 ⟶ 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,570 ⟶ 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 2,218 ⟶ 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,381 ⟶ 2,581:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var mf = Fn.new { |n, d|
9,476

edits