ENع
→Counting
الدرس 10 · الرياضيات المتقطّعة

Counting and combinations

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

How many ways can something happen? Follow one example, five items A to E with three to pick, and count it without listing: the product rule gives 5 × 4 × 3 = 60 ordered picks, dividing by 3! = 6 orders gives 10 teams, and the same 10 appears again from bit patterns, symmetry and Pascal's rule.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 10 · تفاعلي
01 / 10

Five items, pick three

Counting means finding how many ways something can happen, without listing every way. Our example: five items, A to E. We pick three of them to form a team. How many different teams are there?

>>> items = "ABCDE"
>>> len(items)
5
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Five items, pick three

    Counting means finding how many ways something can happen, without listing every way. Our example: five items, A to E. We pick three of them to form a team. How many different teams are there?

    >>> items = "ABCDE"
    >>> len(items)
    5
  2. Product rule: choices multiply

    Fill three slots in turn. The first has 5 choices. A is now used, so the second has 4, then the third has 3. The product rule: when a choice is made in stages, multiply. 5 × 4 × 3 = 60 ordered picks.

    >>> 5 * 4 * 3
    60
    >>> import math
    >>> math.perm(5, 3)       # ordered picks of 3 from 5
    60
  3. Three items, 3! orders

    Order counts in those 60 picks. The same three items stand in 3 × 2 × 1 = 6 orders: ABC, ACB, BAC, BCA, CAB, CBA. We write 3!, said 'three factorial'. In general n! = n × (n − 1) × … × 1.

    >>> from itertools import permutations
    >>> ["".join(p) for p in permutations("ABC")]
    ['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']
    >>> import math
    >>> math.factorial(3)
    6
  4. Order gone: divide by 3!

    A team ignores order: ABC and CBA are the same team. So each team was counted 3! = 6 times among the 60 picks. Divide: 60 ÷ 6 = 10 teams. This is a combination, written C(5, 3) and read '5 choose 3'.

    >>> import math
    >>> math.perm(5, 3) // math.factorial(3)
    10
    >>> math.comb(5, 3)
    10
  5. The choose formula

    Choosing k items from n gives C(n, k) = n! / (k! (n − k)!). The n! / (n − k)! part counts ordered picks; dividing by k! forgets the order. Here 5! = 120, and 120 / (3! × 2!) = 10.

    >>> import math
    >>> f = math.factorial
    >>> f(5) // (f(3) * f(2))
    10
  6. List all ten teams

    Check by listing. A team is a pattern of five lamps with exactly three lit. Python's itertools.combinations lists them in order: ABC, ABD, ABE, and so on to CDE. There are ten, just as the formula said.

    >>> from itertools import combinations
    >>> teams = ["".join(t) for t in combinations("ABCDE", 3)]
    >>> teams
    ['ABC', 'ABD', 'ABE', 'ACD', 'ACE', 'ADE', 'BCD', 'BCE', 'BDE', 'CDE']
    >>> len(teams)
    10
  7. In or out: 2⁵ subsets

    Drop the size limit. Each item is in or out: two choices, five times, so 2⁵ = 32 subsets. Read the lamps as bits: only 10 of the 32 patterns have exactly three lit.

    >>> 2 ** 5
    32
    >>> sum(1 for m in range(32) if bin(m).count("1") == 3)
    10
  8. Choosing 3 = leaving 2

    Picking the 3 who play is the same as picking the 2 who sit out. Flip every lamp and each team of three becomes a pair of two. So C(5, 3) = C(5, 2) = 10. In general, C(n, k) = C(n, n − k).

    >>> import math
    >>> math.comb(5, 3), math.comb(5, 2)
    (10, 10)
  9. Pascal's rule

    Split by cases. Is E in? Yes: pick 2 more from A to D, C(4, 2) = 6. No: pick 3 from A to D, C(4, 3) = 4. No team is in both, so the sum rule adds: 6 + 4 = 10.

    >>> import math
    >>> math.comb(4, 2) + math.comb(4, 3)
    10
    >>> [math.comb(5, k) for k in range(6)]   # row 5 of Pascal's triangle
    [1, 5, 10, 10, 5, 1]
  10. Counting in Python

    Python's math module counts for you: math.comb(5, 3) is 10 and math.perm(5, 3) is 60. Counting first tells you how big a search is before you run it: choosing 3 from 50 already gives 19,600 teams.

    >>> import math
    >>> math.comb(5, 3), math.perm(5, 3)
    (10, 60)
    >>> math.comb(50, 3)
    19600

اختبار يتاح بعد الخطوة الأخيرة.