#/usr/bin/env julia
function factorial( n::Integer )::Integer
if n == 0
error("put zero in factorial")
end
return reduce(*, 1:n)
end
factorial(arr) = Integer(factorial( length(arr) - 1))
#basic permutation P(n,r)
function permut( n, p::Integer = 0 )::Integer
nLength = length(n)
pLength = length(p)
if nLength <= 0 || pLength > nLength
error("illegal input")
end
return Integer( factorial(nLength) / factorial(pLength - p) )
end
#n/(k1!*k2!*k3!) .....
function divide_by_fact(n)::Integer
nLength = length(n)
if nLength <= 0
error("illegal input")
end
nSet = Set(n)
nBag = Dict( key => 0 for key in nSet )
for each = n
nBag[each] += 1
end
step01 = factorial(nLength)
step02 = reduce(*, map(factorial,values(filter((k,v)-> v != 1, nBag))))
return Integer( step01 / step02 )
end
"""
NOT NEEDED
#basic circular permutation
function count(n)::Integer
nLength = length(n)
if nLength <= 0
error("illegal input")
end
return Integer(factorial( nLength - 1))
end
"""
ans1 = Integer(factorial(10) / factorial(5))
ans2 = permut(1:7)
ans3 = divide_by_fact("MASSASAUGA")
ans4 = factorial(1:6)
ans5 = ( factorial(["A","B","C","D","E","F"])
- ( factorial(["AB","C","D","E","F"]) * factorial(2) +
factorial(["AF","B","C","D","E"]) * factorial(2) )
+ factorial(["DAF", "B", "C", "E"]) * factorial(2))
println("""
counting arrangement by factorial
EX:
10!/5! = ?
$(ans1)
counting permutation
EX:
P( 7 , 2 )
P(7, 2) = $(ans2)
EX:
counting the arrangement of MASSASAUGA
10! / 4! * 3! = $(ans3)
EX:
There are 6 people at a party sitting at a round table with 6 seats:
How many ways can the 6 people be seated?
we have $(ans4) to sit
Ex:
There are 6 people at a party sitting at a round table
with 6 seats: A, B, C, D, E and F. A CANNOT sit next to either D or F.
How many ways can the 6 people be seated?
need use Inclusion-exclusion principle
we have $(ans5) way to sit
""")