Sub topic 9
Forming Numbers from Digits
This chapter assumes you know the multiplication principle, factorials, and nPr. It also assumes you can handle restrictions such as "together" and "fixed positions". Here you apply those tools to a very common CAT setting: counting numbers built from a given set of digits.
The Working Model
Counting numbers is a slot-filling problem. You draw one blank per digit position, decide the order in which to fill them, and multiply the number of choices for each slot.
Three questions settle almost every problem:
- Can digits repeat, or must each digit be used at most once?
- Is zero among the digits? Zero cannot lead a number of two or more digits.
- Is there a condition on the last digit (even, divisible by 5) or on the whole number (greater than a bound, divisible by 3)?
The most important rule is to fill the most restricted slot first. If you fill a flexible slot first, the choices left for the restricted slot depend on what you picked, and the count splits into cases.
Case 1: Digits May Repeat
When repetition is allowed, each slot is independent, so the choices do not shrink from slot to slot.
Example: How many 4-digit numbers can be formed from the digits 1, 2, 3, 4, 5 if repetition is allowed?
Each of the 4 slots has 5 choices, so the answer is 5 × 5 × 5 × 5 = 625.
With zero in the digit set, the leading slot loses one choice.
Example: How many 3-digit numbers can be formed from 0, 1, 2, 3, 4 with repetition?
- Hundreds digit: 4 choices (not 0).
- Tens digit: 5 choices.
- Units digit: 5 choices.
The answer is 4 × 5 × 5 = 100.
Case 2: No Repetition
Without repetition, each choice uses up a digit, so later slots have fewer options.
Example: How many 4-digit numbers can be formed from 1, 2, 3, 4, 5 without repetition?
This is 5P4 = 5 × 4 × 3 × 2 = 120.
When Zero Is Present
There are two reliable ways to handle zero. Both must give the same answer, so you can use one to check the other.
Method A: fill the leading slot first. Example: 4-digit numbers from 0, 1, 2, 3, 4, 5 with no repetition.
- Thousands digit: 5 choices (not 0).
- Hundreds digit: 5 choices (the remaining digits, including 0).
- Tens digit: 4 choices.
- Units digit: 3 choices.
The answer is 5 × 5 × 4 × 3 = 300.
Method B: complement. Count all arrangements, then subtract those that start with 0. All arrangements of 4 digits from 6 is 6P4 = 360. Arrangements starting with 0 leave 3 slots filled from 5 digits, so 5P3 = 60. The answer is 360 − 60 = 300.
The two methods agree, which confirms the count.
Case 3: Conditions on the Last Digit
For "even", "odd", or "divisible by 5" conditions, fill the units slot first. After that, fill the leading slot, and then the middle slots.
Example: How many 3-digit even numbers can be formed from 0, 1, 2, 3, 4, 5 without repetition?
The units digit must be 0, 2, or 4. Zero in the units place is a special case because it does not affect the leading slot, so split into two cases.
- Units = 0: The hundreds digit has 5 choices and the tens digit has 4. That gives 5 × 4 = 20.
- Units = 2 or 4 (2 choices): The hundreds digit cannot be 0 or the digit already used, so it has 4 choices. The tens digit has 4 choices (the remaining digits, now including 0). That gives 2 × 4 × 4 = 32.
The total is 20 + 32 = 52.
from itertools import permutations
digits = [0, 1, 2, 3, 4, 5]
count = 0
for p in permutations(digits, 3):
if p[0] != 0 and p[2] % 2 == 0:
count += 1
print(count) # 52Brute force is a good way to check a formula when you practice. It is not available in the exam, so you must be able to do the case split by hand.
Case 4: Divisibility Rules
Divisibility conditions turn into conditions on the digits chosen.
| Divisor | Condition on the number | What to do when counting |
|---|---|---|
| 2 | Last digit is even | Fill the units slot first |
| 5 | Last digit is 0 or 5 | Fill the units slot first |
| 4 | Last two digits form a multiple of 4 | List valid endings, then fill the rest |
| 3 | Digit sum is a multiple of 3 | Choose valid sets of digits, then arrange each set |
| 9 | Digit sum is a multiple of 9 | Same as for 3 |
Divisibility by 3: Choose the Set, Then Arrange It
Example: How many 3-digit numbers divisible by 3 can be formed from 1, 2, 3, 4, 5 without repetition?
Find the 3-element subsets whose sum is a multiple of 3. Possible sums run from 1+2+3 = 6 to 3+4+5 = 12, so the targets are 6, 9, and 12.
- Sum 6: {1, 2, 3}
- Sum 9: {1, 3, 5}, {2, 3, 4}
- Sum 12: {3, 4, 5}
That gives 4 sets. Each set has no zero, so each can be arranged in 3! = 6 ways. The answer is 4 × 6 = 24.
from itertools import combinations
from math import factorial
sets = [c for c in combinations([1, 2, 3, 4, 5], 3) if sum(c) % 3 == 0]
print(sets) # [(1, 2, 3), (1, 3, 5), (2, 3, 4), (3, 4, 5)]
print(len(sets) * factorial(3)) # 24If a chosen set contains 0, only 2 × 2! = 4 of its 3! = 6 arrangements are valid 3-digit numbers, because 0 cannot be first.
Case 5: Numbers Greater or Smaller Than a Bound
For "greater than N" problems, count by the first position where the number can exceed the bound, or count the complement.
Example: How many 4-digit numbers greater than 3000 can be formed from 1, 2, 3, 4, 5 without repetition?
A 4-digit number is greater than 3000 when its thousands digit is 3, 4, or 5. That is 3 choices. The other three slots are filled from the remaining 4 digits: 4 × 3 × 2 = 24. The answer is 3 × 24 = 72.
Check with the complement. All numbers: 5P4 = 120. Numbers starting with 1 or 2: 2 × 24 = 48. Then 120 − 48 = 72, which matches.
Case 6: Counting All Numbers of Several Lengths
When a problem asks for "all numbers" that can be formed, add the counts for each length.
Example: How many numbers of at most 3 digits can be formed from 1, 2, 3 without repetition?
- 1-digit: 3P1 = 3
- 2-digit: 3P2 = 6
- 3-digit: 3P3 = 6
The total is 3 + 6 + 6 = 15.
Case 7: Sum of All the Numbers Formed
A frequent CAT variation asks for the sum of all numbers formed. Use symmetry. If digits are distinct and none is zero, each digit appears in each position equally often.
Example: Find the sum of all 3-digit numbers formed from 1, 2, 3 without repetition.
There are 3! = 6 numbers. Each digit appears in each position 6 / 3 = 2 times. The sum of the digits in one position is (1 + 2 + 3) × 2 = 12. Each position contributes its place value, so the total is 12 × (100 + 10 + 1) = 12 × 111 = 1332.
from itertools import permutations
total = sum(int("".join(map(str, p))) for p in permutations([1, 2, 3]))
print(total) # 1332If zero is among the digits, this symmetry fails for the leading position. Handle the cases separately or use the complement.
Common Errors
- Letting 0 lead. Always check the first slot when zero is in the set.
- Filling a flexible slot before a restricted one. This produces choices that depend on earlier picks and causes miscounts.
- Forgetting the zero special case in "even" problems. Zero in the units place does not use up a nonzero leading digit, so it needs its own case.
- Counting sets as arrangements. For divisibility by 3, choosing a set gives combinations. You must still arrange the digits.
- Ignoring repeated digits in the given set. If the digit set has a repeat, such as 1, 1, 2, 3, divide by the factorial of the repeat count, as in the earlier chapter on identical objects.
Choosing a Method Quickly
| Situation | Best approach |
|---|---|
| Repetition allowed | Multiply the choices slot by slot |
| No repetition, no zero | Use nPr |
| No repetition, zero present | Fill the leading slot first, or use total minus leading-zero count |
| Even or multiple of 5 | Fill the units slot first, splitting on 0 |
| Divisible by 3 or 9 | Pick valid digit sets, then arrange |
| Greater than a bound | Split by leading digit, or use the complement |
| Sum of all numbers | Use positional symmetry |
Exercise
How many 4-digit numbers divisible by 5 can be formed from 0, 1, 2, 3, 5 without repetition?
Show answer
The units digit is 0 or 5. If units = 0, the other three slots are filled from 1, 2, 3, 5 in order: 4 × 3 × 2 = 24. If units = 5, the thousands digit has 3 choices (1, 2, 3, since 0 and 5 are unavailable), and the hundreds and tens digits are filled from the 3 remaining digits (including 0): 3 × 3 × 2 = 18. The total is 24 + 18 = 42.
Exercise
Find the sum of all 4-digit numbers formed from 1, 2, 3, 4 without repetition.
Show answer
There are 4! = 24 numbers. Each digit appears in each position 24 / 4 = 6 times. The digit sum per position is (1 + 2 + 3 + 4) × 6 = 60. The total is 60 × 1111 = 66660.
Exercise
How many 3-digit numbers can be formed from 0, 1, 2, 3 with repetition allowed that are odd?
Show answer
The units digit must be 1 or 3, which is 2 choices. The hundreds digit cannot be 0, so it has 3 choices. The tens digit has 4 choices. The total is 3 × 4 × 2 = 24.