How to resolve the algorithm Combinations and permutations step by step in the Erlang programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Combinations and permutations step by step in the Erlang programming language

Table of Contents

Problem Statement

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

See the Wikipedia articles for a more detailed description. To test, generate and print examples of:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Combinations and permutations step by step in the Erlang programming language

Source code in the erlang programming language

-module(combinations_permutations).

-export([test/0]).

perm(N, K) ->
    product(lists:seq(N - K + 1, N)).

comb(N, K) ->
    perm(N, K) div product(lists:seq(1, K)).

product(List) ->
    lists:foldl(fun(N, Acc) -> N * Acc end, 1, List).

test() ->
    io:format("\nA sample of permutations from 1 to 12:\n"),
    [show_perm({N, N div 3}) || N <- lists:seq(1, 12)],
    io:format("\nA sample of combinations from 10 to 60:\n"),
    [show_comb({N, N div 3}) || N <- lists:seq(10, 60, 10)],
    io:format("\nA sample of permutations from 5 to 15000:\n"),
    [show_perm({N, N div 3}) || N <- [5,50,500,1000,5000,15000]],
    io:format("\nA sample of combinations from 100 to 1000:\n"),
    [show_comb({N, N div 3}) || N <- lists:seq(100, 1000, 100)],
    ok.

show_perm({N, K}) ->
    show_gen(N, K, "perm", fun perm/2).

show_comb({N, K}) ->
    show_gen(N, K, "comb", fun comb/2).

show_gen(N, K, StrFun, Fun) ->
    io:format("~s(~p, ~p) = ~s\n",[StrFun, N, K, show_big(Fun(N, K), 40)]).

show_big(N, Limit) ->
    StrN = integer_to_list(N),
    case length(StrN) < Limit of
        true ->
            StrN;
        false -> 
            {Shown, Hidden} = lists:split(Limit, StrN),
            io_lib:format("~s... (~p more digits)", [Shown, length(Hidden)]) 
    end.


  

You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the Ruby programming language
You may also check:How to resolve the algorithm Partition function P step by step in the Rust programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the SQL programming language
You may also check:How to resolve the algorithm Bin given limits step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Non-transitive dice step by step in the Factor programming language