Raw

2. Methodical guidelines / Методичні вказівки

This section is self-contained: it develops the theory of the work’s theme — combinatorial counting and descriptive statistics — together with algorithms for the tasks in 4task.md. No outside material is required.

2.1 Permutations, arrangements, combinations

Let nn be the number of available elements and kk the size of a selection (0kn0 \le k \le n).

  • A permutation is an ordering of all nn elements. Their number is

    Pn=n!=n(n1)21.P_n = n! = n\cdot(n-1)\cdots 2\cdot 1.

  • An arrangement (written AnkA_n^k here; the same count is P(n,k)P(n,k) in Lecture 13 and Practical 6) is an ordered selection of kk elements (order matters):

    Ank=n(n1)(nk+1)=n!(nk)!.A_n^k = n\cdot(n-1)\cdots(n-k+1) = \frac{n!}{(n-k)!}.

  • A combination is an unordered selection of kk elements (order does not matter):

    Cnk=Ankk!=n!k!(nk)!=(nk).C_n^k = \frac{A_n^k}{k!} = \frac{n!}{k!\,(n-k)!} = \binom{n}{k}.

The single distinction is order: arrangements count (A,B)(A,B) and (B,A)(B,A) separately; combinations treat them as the same selection. For n=5, k=3n = 5,\ k = 3: P5=120P_5 = 120, A53=1202=60A_5^3 = \tfrac{120}{2} = 60, and C53=606=10C_5^3 = \tfrac{60}{6} = 10.

2.2 Computing the counts

Compute a factorial with a simple loop. For AnkA_n^k and CnkC_n^k, prefer the multiplicative form over dividing two large factorials — it keeps the numbers small and exact:

function factorial(n):
    r ← 1
    for i from 2 to n: r ← r * i
    return r

function arrangements(n, k):             # A(n,k) = n·(n-1)···(n-k+1)
    r ← 1
    for i from 0 to k-1: r ← r * (n - i)
    return r

function combinations(n, k):             # C(n,k) = A(n,k) / k!
    return arrangements(n, k) / factorial(k)

2.3 Descriptive statistics

For a dataset x1,,xnx_1, \dots, x_n:

  • the mean (mathematical expectation) is the average value

    μ=E[X]=1ni=1nxi;\mu = E[X] = \frac{1}{n}\sum_{i=1}^{n} x_i;

  • the variance measures the average squared spread about the mean

    D[X]=Var(X)=σ2=1ni=1n(xiμ)2;D[X] = \operatorname{Var}(X) = \sigma^2 = \frac{1}{n}\sum_{i=1}^{n} (x_i - \mu)^2;

  • the standard deviation is its square root, in the same units as the data, σ=D[X]\sigma = \sqrt{D[X]}.

(The formula above is the population variance, dividing by nn; a sample variance divides by n1n-1.) For the dataset {2,4,4,4,5,5,7,9}\{2,4,4,4,5,5,7,9\}: μ=40/8=5\mu = 40/8 = 5, the squared deviations sum to 3232, so D[X]=32/8=4D[X] = 32/8 = 4 and σ=2\sigma = 2.

Expectation as the weighted-average balance point of a distribution: E[X] = 3.5 for a fair die

2.4 Generating combinatorial objects

Counting says how many; generating produces the objects themselves. Permutations are generated by recursion — fix each element in turn as the first, and permute the rest:

function permutations(elements):
    if length(elements) ≤ 1: return [ elements ]
    result ← empty list
    for i from 0 to length(elements) - 1:
        first ← elements[i]
        rest  ← elements without index i
        for p in permutations(rest):
            append [first] + p to result
    return result

If elements is sorted, this emits them in lexicographic order: (A,B,C),(A,C,B),(B,A,C),(B,C,A),(C,A,B),(C,B,A)(A,B,C), (A,C,B), (B,A,C), (B,C,A), (C,A,B), (C,B,A) — six in total (3!=63! = 6). Combinations of size kk are generated similarly: for each element, either include it (and choose k1k-1 from the rest) or skip it.

2.5 Complexity

Computing PnP_n, AnkA_n^k, or CnkC_n^k takes O(n)O(n) (or O(k)O(k)) multiplications. The statistics take one or two passes over the data, O(n)O(n). Generating all permutations is inherently O(nn!)O(n \cdot n!) — the output alone has n!n! items — so explicit generation is practical only for small nn.

Laboratory/Laboratory7/2method.md · 3.6 KB · updated 2026-08-01 18:44