// COMBINATORICS · INTERMEDIATE

Permutations & Combinations

Factorials, nPr, nCr, Pascal's triangle, and the binomial theorem — the counting toolkit behind algorithm analysis.

Intermediate·20 min·Mathematics · Combinatorics
Reviewed by CodeTikki Academic Team

Prerequisites: Basic arithmetic · Algebra · Exponents

// VISUALIZER

Build Pascal's triangle step by step

Each entry is the sum of the two above it. Entry (n, r) equals C(n, r). Each row sums to 2^n. Watch the triangle grow.

1
1
1
1
2
1
1
3
3
1
1
4
6
4
1
1
5
10
10
5
1

Each entry is the sum of the two above it. Entry (n, r) = C(n, r). Row n sums to 2^n.

Row sum

2^5 = 32

Total entries

21

// CALCULATOR

nPr vs nCr — see the difference

Pick n and r. Compare permutations (order matters) with combinations (order doesn't). Each combination produces r! permutations.

Permutation (order matters)

8P3 = 8! / (8-3)!

336

Combination (order doesn't matter)

8C3 = 8! / (3! × (8-3)!)

56

8P3 = 336 arrangements (ordered). 8C3 = 56 selections (unordered). The ratio is r! = 6 — each combination produces 6 permutations.

// MINI GAME

Counting Quest

Solve counting problems against the clock! Factorials, permutations, combinations, circular arrangements, and the counting principle. Use hints wisely — they cost XP.

Loading quest...

// FLOWCHART

Algorithm flow

// FLOWCHART · Permutations & Combinations
Decide and compute nPr or nCr
YesNoYesNoStartRead n, r0 ≤ r ≤ n?Does ordermatter?Compute nPr= n! / (n-r)!Compute nCr= n! / (r!(n-r)!)Return resultInvalid inputEnd

Press PLAY to trace the algorithm through the flowchart.

0 / 8

// PSEUDOCODE

Trace the code

// PSEUDOCODE · Compute nCr
Binomial coefficient for n=8, r=3
n = 8, r = 3
1function nCr(n, r):
2 if r < 0 or r > n:
3 return 0
4 if r == 0 or r == n:
5 return 1
6 result = 1
7 for i in 0..r-1:
8 result = result * (n - i)
9 result = result / (i + 1)
10 return result

Press PLAY to step through the algorithm line by line.

0 / 13

// TUTORIAL QUIZZES

Test your mastery

From the fundamental counting principle to Pascal's triangle, derangements, and competitive programming shortcuts.

// PRACTICE & ASSESS

Test your understanding

Now that you've learned the concept, put it into practice. Solve coding problems and take quizzes to reinforce what you've learned.

// REFERENCES

Sources & further reading

  1. [1]
    Pascal's TriangleBlaise Pascal (1653) — Traité du triangle arithmétique
  2. [2]
    Ars ConjectandiJacob Bernoulli — combinatorics and probability
  3. [3]
    Introduction to Algorithms (CLRS)T. H. Cormen et al. — combinatorial analysis and counting
  4. [4]
    Concrete MathematicsRonald L. Graham, Donald E. Knuth, Oren Patashnik — combinatorics for CS

// READY?

Distribute identical objects into bins

Next up: Stars and Bars — a classic combinatorics technique for counting distributions.