2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -13,4 +13,5 @@ If we define the degree of the multifactorial as the difference in successive te
# 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.
<small>'''Note:''' The [[wp:Factorial#Multifactorials|wikipedia entry on multifactorials]] gives a different formula. This task uses the [http://mathworld.wolfram.com/Multifactorial.html Wolfram mathworld definition].</small>
'''Note:''' The [[wp:Factorial#Multifactorials|wikipedia entry on multifactorials]] gives a different formula. This task uses the [http://mathworld.wolfram.com/Multifactorial.html Wolfram mathworld definition].

View file

@ -0,0 +1,51 @@
* 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

View file

@ -1,10 +1,10 @@
defmodule RC do
def multifactorial(n,d) do
List.foldl(:lists.seq(n,1,-d), 1, fn x,p -> x*p end)
Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end)
end
end
Enum.each(1..5, fn d ->
multifac = Enum.map(1..10, fn n -> RC.multifactorial(n,d) end)
multifac = for n <- 1..10, do: RC.multifactorial(n,d)
IO.puts "Degree #{d}: #{inspect multifac}"
end)

View file

@ -0,0 +1,2 @@
: !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 ;

View file

@ -0,0 +1,23 @@
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

View file

@ -0,0 +1,14 @@
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()
}
}

View file

@ -0,0 +1,17 @@
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

View file

@ -1,8 +1,14 @@
use 5.10.0;
sub ng {
state %g;
my ($n, $d, $key) = ( @_[0], @_[1], $n.'ng'.$d);
if (!$g{$key}) {$g[$key] = ($n <= $d+1)? $n : ng($n-$d,$d)*$n}
return $g[$key];
{ # <-- 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";
}

View file

@ -1,9 +1,10 @@
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,1), ng(2,1), ng(3,1), ng(4,1), ng(5,1), ng(6,1), ng(7,1), ng(8,1), ng(9,1), ng(10,1) ;
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,2), ng(2,2), ng(3,2), ng(4,2), ng(5,2), ng(6,2), ng(7,2), ng(8,2), ng(9,2), ng(10,2) ;
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,3), ng(2,3), ng(3,3), ng(4,3), ng(5,3), ng(6,3), ng(7,3), ng(8,3), ng(9,3), ng(10,3) ;
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,4), ng(2,4), ng(3,4), ng(4,4), ng(5,4), ng(6,4), ng(7,4), ng(8,4), ng(9,4), ng(10,4) ;
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,5), ng(2,5), ng(3,5), ng(4,5), ng(5,5), ng(6,5), ng(7,5), ng(8,5), ng(9,5), ng(10,5) ;
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,6), ng(2,6), ng(3,6), ng(4,6), ng(5,6), ng(6,6), ng(7,6), ng(8,6), ng(9,6), ng(10,6) ;
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,7), ng(2,7), ng(3,7), ng(4,7), ng(5,7), ng(6,7), ng(7,7), ng(8,7), ng(9,7), ng(10,7) ;
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,8), ng(2,8), ng(3,8), ng(4,8), ng(5,8), ng(6,8), ng(7,8), ng(8,8), ng(9,8), ng(10,8) ;
printf "%s %s %s %s %s %s %s %s %s %s\n", ng(1,9), ng(2,9), ng(3,9), ng(4,9), ng(5,9), ng(6,9), ng(7,9), ng(8,9), ng(9,9), ng(10,9) ;
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);
}

View file

@ -1,14 +0,0 @@
{ # <-- 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";
}

View file

@ -0,0 +1,11 @@
(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) )

View file

@ -0,0 +1,9 @@
#x is Input
#n is Factorial Number
multifactorial=function(x,n){
if(x<=n+1){
return(x)
}else{
return(x*multifactorial(x-n,n))
}
}

View file

@ -1,21 +1,18 @@
/*REXX program calculates 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
/*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*/ /*(above) D can default to unity. */
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))':' _
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 /*j*/
return !
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 !

View file

@ -0,0 +1,17 @@
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

View file

@ -0,0 +1,6 @@
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")