☰

Permutation

Permutation: Definition, Formula and Solved Examples

You are setting a four digit PIN for a new phone. The PIN 1234 and the PIN 4321 use the same digits, but they are different PINs, because the order matters. How many different PINs are possible? Counting problems like this, where the order of the items matters, are what permutations are for.

Permutations are the first tool in counting, and they quietly power a lot of data work. They tell you how many ways to rank items, how many passwords an attacker must try, how many test orders a schedule allows, and how large a search space is before you design an algorithm. They are also the base for combinations and for probability.

This tutorial explains what a permutation is, the formula and the factorial behind it, worked examples with and without repeated items, how it differs from a combination, and how to check your counts in Python. You only need basic multiplication.

What is a Permutation?

A permutation is an arrangement of objects in a definite order. When you choose r objects out of n, and the order matters, each different ordered arrangement is one permutation. The word to remember is order. Choosing A then B is different from choosing B then A.

Example: from the letters A, B and C, take two at a time. The permutations are AB, AC, BA, BC, CA and CB. That is 6 arrangements. Note that AB and BA are counted separately.

Factorials in Two Minutes

The permutation formula uses factorials. n factorial, written n!, is the product of all whole numbers from n down to 1.

  • 5! = 5 times 4 times 3 times 2 times 1 = 120
  • 3! = 6
  • 0! = 1, by agreement. This keeps the formulas working.

Why Use Permutations?

Use a permutation when you must count ordered outcomes, such as these.

  • Ranking: the first, second and third place in a race of 10 runners.
  • Codes: PINs, passwords and locker codes, where each position matters.
  • Seating and scheduling: who sits where, which task runs first.
  • Splitting people or items into labelled groups, such as the cousins problem below.

The live page uses this problem: eight cousins must be accommodated in two rooms with 3 beds each and one room with 2 beds. In how many ways can they be placed? It is solved below, after the formula.

Permutation Formula

The number of permutations of r objects chosen from n different objects is:

nPr = n! divided by (n minus r)!
Permutation formula

It is the same as multiplying r numbers, starting at n and going down. For example, 10P3 = 10 times 9 times 8 = 720. The first position has 10 choices, the second has 9 left, and the third has 8.

Permutation formulas by case
Case Formula
All n objects arranged n! (this is nPn)
r out of n objects, no repeats n! divided by (n minus r)!
r positions, repeats allowed n to the power r
n objects where some are identical n! divided by (p! times q! times and so on), where p, q and so on count the identical items
n objects around a circle (n minus 1)!

Permutation Examples

Example 1: Choosing Three Office Bearers

A club of 10 members elects a president, a vice president and a secretary. The roles are different, so order matters. The number of ways is 10P3 = 10 times 9 times 8 = 720.

Example 2: Arranging All Objects

Five people line up for a photo. The number of orders is 5! = 120.

Example 3: PIN Codes

Four digit PIN: with and without repeated digits
Question Count
4 digit PIN, digits can repeat 10 to the power 4 = 10,000
4 digit PIN, no digit repeated 10P4 = 10 times 9 times 8 times 7 = 5,040

Not allowing repeats cuts the choices by half. This is why many systems forbid PINs like 1111.

Example 4: Arrangements with Repeated Letters

How many different arrangements are there of the letters of BOOK? There are 4 letters, but the two O’s are identical, so swapping them gives nothing new. The count is 4! divided by 2! = 24 divided by 2 = 12.

For the word LETTER, there are 6 letters with two E’s and two T’s. The count is 6! divided by (2! times 2!) = 720 divided by 4 = 180.

Example 5: The Cousins Problem

Eight cousins must be placed in rooms with 3, 3 and 2 beds. Treat the rooms as different. Give each cousin a room label: A for the first 3 bed room, B for the second, and C for the 2 bed room. A placement is then an arrangement of the letters AAABBBCC. This is a permutation with repeated items:

8! divided by (3! times 3! times 2!) = 40,320 divided by (6 times 6 times 2) = 40,320 divided by 72 = 560 ways.

Permutation example: eight cousins placed in three rooms

I checked this by listing every distinct arrangement of AAABBBCC in Python, which gave 560. If the two 3 bed rooms are interchangeable, so that swapping the groups of the two rooms gives the same plan, divide by 2 and the answer is 280. The live page states no answer, so state which assumption you use.

Example 6: Restrictions

Five people A, B, C, D and E sit in a row. In how many ways can A and B sit next to each other? Treat A and B as one block. The block and the other three people make 4 units, which can be arranged in 4! = 24 ways. Inside the block, A and B can swap, so multiply by 2. The answer is 48. A Python count of all 120 orders, keeping those with A and B side by side, also gives 48.

Example 7: A Round Table

Six people sit around a round table. Rotating everyone one seat gives the same arrangement, so fix one person and arrange the other five. The count is (6 minus 1)! = 120, not 720.

Permutation vs Combination

Permutation and combination compared
Feature Permutation Combination
Does order matter? Yes No
Question How many ordered arrangements? How many selections or groups?
Formula n! divided by (n minus r)! n! divided by (r! times (n minus r)!)
Example, 3 out of 10 720 120
Example President, vice president, secretary A committee of 3

The link: nPr = nCr times r!. For 10 and 3, 120 times 6 = 720. Each group of 3 people can be ordered in 3! = 6 ways. A quick test: if swapping two items gives a different outcome, use a permutation. If it gives the same outcome, use a combination.

Where Permutations Are Used in Data Science

  • Search space size. Before trying every ordering of features or tasks, you can count how many there are, and see why brute force fails. Ten tasks already have 3,628,800 orders.
  • Permutation tests. A statistical test that shuffles labels many times to see how unusual a result is.
  • Password and key strength. Counting the number of possible codes.
  • Probability. Many probability problems start by counting ordered outcomes.

Check It in Python

Python has permutation tools built in. I ran the code below, and the output is real.

python
import math
from itertools import permutations

print("5! =", math.factorial(5))
print("10P3 =", math.perm(10, 3))
print("4 digit PIN, no repeats:", math.perm(10, 4))
print("Cousins:", math.factorial(8) // (math.factorial(3) * math.factorial(3) * math.factorial(2)))
print("Check by listing:", len(set(permutations("AAABBBCC"))))
print("BOOK:", len(set(permutations("BOOK"))))
print(["".join(p) for p in permutations("ABC", 2)])
output
5! = 120
10P3 = 720
4 digit PIN, no repeats: 5040
Cousins: 560
Check by listing: 560
BOOK: 12
['AB', 'AC', 'BA', 'BC', 'CA', 'CB']

Take care with the listing method. It creates every arrangement, so it is only practical for small cases. For 8 letters it is instant, and for 15 or more items it would run out of memory. Use the formula for large counts.

Common Mistakes

Common mistakes and the fix
Mistake Fix
Using a permutation when order does not matter Use a combination
Forgetting to divide for identical items Divide by the factorial of each repeated count
Treating nPr as n times r Multiply r numbers going down from n
Using (n minus 1)! for a straight line That is only for circles
Forgetting that 0! = 1 Remember it, so that nPn works
Not stating assumptions, such as rooms being identical Say whether the groups are labelled or not

Practice Questions

  1. In how many ways can 4 books be arranged on a shelf?
  2. Find 8P2.
  3. How many different arrangements are there of the letters of the word LEVEL?
  4. Seven people sit around a round table. In how many ways?

Answers: 1) 4! = 24. 2) 8 times 7 = 56. 3) LEVEL has 5 letters, with L twice and E twice. 5! divided by (2! times 2!) = 120 divided by 4 = 30. 4) (7 minus 1)! = 720. These are my own calculations, so please recheck them before publishing.

Permutations come down to one question: does the order matter? If yes, count positions one at a time and multiply the choices, or use the formula. Work through a few small cases by hand, confirm them in Python, and the formulas will feel natural. Next, learn combinations and see how the two are linked.

Popular Tags:

permuation and combination permutation example permutation formula Permutation tutorial