

 This page uses content from Wikipedia. The original article was at Combination. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



 This page uses content from Wikipedia. The original article was at Permutation. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)

Task

Implement the combination    (nCk)    and permutation    (nPk)    operators in the target language:

 






n



C

k


=



(


n
k


)



=



n
(
n
−
1
)
…
(
n
−
k
+
1
)


k
(
k
−
1
)
…
1





{\displaystyle ^{n}\operatorname {C} _{k}={\binom {n}{k}}={\frac {n(n-1)\ldots (n-k+1)}{k(k-1)\dots 1}}}


 






n



P

k


=
n
⋅
(
n
−
1
)
⋅
(
n
−
2
)
⋯
(
n
−
k
+
1
)


{\displaystyle ^{n}\operatorname {P} _{k}=n\cdot (n-1)\cdot (n-2)\cdots (n-k+1)}




See the Wikipedia articles for a more detailed description.

To test, generate and print examples of:

   A sample of permutations from 1 to 12 and Combinations from 10 to 60 using exact Integer arithmetic.
   A sample of permutations from 5 to 15000 and Combinations from 100 to 1000 using approximate Floating point arithmetic.
 This 'floating point' code could be implemented using an approximation, e.g., by calling the Gamma function.


Related task

   Evaluate binomial coefficients



 The number of samples of size k from n objects.


With   combinations and permutations   generation tasks. 






 Order Unimportant

 Order Important


 Without replacement








(


n
k


)




=

n



C

k


=



n
(
n
−
1
)
…
(
n
−
k
+
1
)


k
(
k
−
1
)
…
1





{\displaystyle {\binom {n}{k}}=^{n}\operatorname {C} _{k}={\frac {n(n-1)\ldots (n-k+1)}{k(k-1)\dots 1}}}










n



P

k


=
n
⋅
(
n
−
1
)
⋅
(
n
−
2
)
⋯
(
n
−
k
+
1
)


{\displaystyle ^{n}\operatorname {P} _{k}=n\cdot (n-1)\cdot (n-2)\cdots (n-k+1)}




Task: Combinations

Task: Permutations


 With replacement








(



n
+
k
−
1

k


)




=

n
+
k
−
1



C

k


=



(
n
+
k
−
1
)
!


(
n
−
1
)
!
k
!





{\displaystyle {\binom {n+k-1}{k}}=^{n+k-1}\operatorname {C} _{k}={(n+k-1)! \over (n-1)!k!}}








n

k




{\displaystyle n^{k}}




Task: Combinations with repetitions

Task: Permutations with repetitions



defmodule Combinations_permutations do
  def perm(n, k), do: product(n - k + 1 .. n)
 
  def comb(n, k), do: div( perm(n, k), product(1 .. k) )
 
  defp product(a..b) when a>b, do: 1
  defp product(list), do: Enum.reduce(list, 1, fn n, acc -> n * acc end)
 
  def test do
    IO.puts "\nA sample of permutations from 1 to 12:"
    Enum.each(1..12, &show_perm(&1, div(&1, 3)))
    IO.puts "\nA sample of combinations from 10 to 60:"
    Enum.take_every(10..60, 10) |> Enum.each(&show_comb(&1, div(&1, 3)))
    IO.puts "\nA sample of permutations from 5 to 15000:"
    Enum.each([5,50,500,1000,5000,15000], &show_perm(&1, div(&1, 3)))
    IO.puts "\nA sample of combinations from 100 to 1000:"
    Enum.take_every(100..1000, 100) |> Enum.each(&show_comb(&1, div(&1, 3)))
  end
 
  defp show_perm(n, k), do: show_gen(n, k, "perm", &perm/2)
 
  defp show_comb(n, k), do: show_gen(n, k, "comb", &comb/2)
 
  defp show_gen(n, k, strfun, fun), do:
    IO.puts "#{strfun}(#{n}, #{k}) = #{show_big(fun.(n, k), 40)}"
 
  defp show_big(n, limit) do
    strn = to_string(n)
    if String.length(strn) < limit do
      strn
    else
      {shown, hidden} = String.split_at(strn, limit)
      "#{shown}... (#{String.length(hidden)} more digits)"
    end
  end
end
 
Combinations_permutations.test