Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,15 +1,16 @@
The factorial of a number, written as <math>n!</math> is defined as <math>n! = n(n-1)(n-2)...(2)(1)</math>
The factorial of a number, written as <math>n!</math>, is defined as <math>n! = n(n-1)(n-2)...(2)(1)</math>.
A generalization of this is the [http://mathworld.wolfram.com/Multifactorial.html multifactorials] where:
[http://mathworld.wolfram.com/Multifactorial.html Multifactorials] generalize factorials as follows:
: <math>n! = n(n-1)(n-2)...(2)(1)</math>
: <math>n!! = n(n-2)(n-4)...</math>
: <math>n!! ! = n(n-3)(n-6)...</math>
: <math>n!! !! = n(n-4)(n-8)...</math>
: <math>n!! !! ! = n(n-5)(n-10)...</math>
: Where the products are for 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 to
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 1..10 members of the first five degrees of multifactorial.
# Use the function to generate and display here a table of the first ten members (1 to 10) of the first five degrees of multifactorial.
<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>

View file

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

View file

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

View file

@ -0,0 +1,18 @@
function multifact{T<:Integer,U<:Integer}(n::T, k::U)
-1<n && 0<k || throw(DomainError())
1 < k || return factorial(n)
r = one(T)
for i in n:-k:2
r *= i
end
return r
end
khi = 5
nhi = 10
println("Showing multifactorial for n in [1,", nhi, "] and k in [1,", khi, "].")
for k = 1:khi
a = Int64[multifact(i, k) for i in 1:nhi]
lab = "n"*"!"^k
println(@sprintf(" %-6s => ", lab), a)
end

View file

@ -1,21 +1,21 @@
/*REXX pgm calculates K-fact (multifactorial) of non-negative integers.*/
numeric digits 1000 /*lets get ka-razy with precision*/
parse arg num deg . /*allow user to specify num & deg*/
if num=='' | num==',' then num=15 /*Not specified? Then use default*/
if deg=='' | deg==',' then deg=10 /* " " " " " */
say 'showing multiple factorials (1 ' deg") for numbers 1 ──►" num
/*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
say
do d=1 for deg /*the factorializing (º) of !'s. */
_= /*the list of factorials so far. */
do f=1 for num /* ◄── do a ! from 1 to num.*/
_=_ Kfact(f,d) /*construct a list of factorials.*/
end /*f*/ /*(above) D can default to 1.*/
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. */
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 done.*/
/*──────────────────────────────────KFACT subroutine────────────────────*/
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 /*j*/
return !

View file

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