vampire numbers
number theory
mathematics
4-digit numbers
mathematical curiosities

Find all the 4 digit vampire numbers

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A four-digit vampire number is a four-digit integer whose digits can be rearranged to form two two-digit factors, called fangs, whose product is the original number. The standard definition also says the two fangs cannot both end in zero.

This is a recreational math problem, but it is also a good small search problem. The clean way to solve it is to iterate through all two-digit factor pairs and compare sorted digits instead of trying to reason about each number by hand.

What Makes a Number a Vampire Number

For a four-digit number n, we want two two-digit integers a and b such that:

  • 'a * b == n'
  • the multiset of digits in a and b matches the digits of n
  • 'a and b do not both end in zero'

For example, 1260 qualifies because:

text
21 * 60 = 1260
digits of 21 and 60 -> 2, 1, 6, 0
digits of 1260      -> 1, 2, 6, 0

The digits match after sorting, so 1260 is a vampire number.

A straightforward solution is to try every pair of two-digit numbers. There are only 90 * 90 possible pairs from 10 to 99, so brute force is perfectly reasonable here.

python
1def is_vampire(n, a, b):
2    if a * b != n:
3        return False
4
5    if a % 10 == 0 and b % 10 == 0:
6        return False
7
8    fang_digits = sorted(str(a) + str(b))
9    number_digits = sorted(str(n))
10    return fang_digits == number_digits
11
12
13def find_four_digit_vampires():
14    results = []
15
16    for a in range(10, 100):
17        for b in range(a, 100):
18            n = a * b
19            if 1000 <= n <= 9999 and is_vampire(n, a, b):
20                results.append((n, a, b))
21
22    return results
23
24
25for number, a, b in find_four_digit_vampires():
26    print(f"{number} = {a} * {b}")

This prints:

text
11260 = 21 * 60
21395 = 15 * 93
31435 = 35 * 41
41530 = 30 * 51
51827 = 21 * 87
62187 = 27 * 81
76880 = 80 * 86

Those are the classic four-digit vampire numbers.

Why the Factor Loop Is Better Than Scanning All Numbers

You could loop from 1000 to 9999 and test all factor pairs for each number, but that repeats unnecessary work. Looping through factor pairs directly is cleaner:

  • you automatically generate candidate products
  • you avoid factoring every four-digit number
  • the search space stays small and easy to reason about

The upper bound is tiny anyway, but the factor-pair formulation matches the mathematical definition more closely.

Returning Unique Results

The code above starts the inner loop at b = a, which prevents duplicates such as both 21 * 60 and 60 * 21. That gives each vampire number one canonical factor pair.

If you want every valid fang pair, remove that restriction:

python
1def all_fang_pairs(n):
2    pairs = []
3
4    for a in range(10, 100):
5        for b in range(10, 100):
6            if is_vampire(n, a, b):
7                pairs.append((a, b))
8
9    return pairs

For most uses, though, unique pairs are enough.

Complexity

The brute force pair search runs in constant time for this exact problem size because the bounds are fixed. More generally, it performs about O(k^2) pair checks for k-digit fangs.

For four-digit vampire numbers, that is only a few thousand checks. Simplicity is more valuable here than trying to invent a more elaborate optimization.

Mathematical Notes

Vampire numbers are mostly a curiosity, but they are a nice example of combining:

  • digit counting
  • factorization
  • search pruning

They also show how a clean computational approach can confirm a finite mathematical list quickly and reliably.

If you want to generalize beyond four digits, the same idea still works, but the search space grows and you start caring more about pruning candidate factors.

Common Pitfalls

The most common mistake is forgetting the rule about trailing zeros. The usual definition disallows both fangs ending with zero, so 20 * 63 is fine but 20 * 60 would not be.

Another mistake is checking digits without sorting or counting them properly. Since order does not matter, compare multisets of digits, not the raw strings.

People also duplicate results by checking both factor orders. Starting the inner loop at a avoids that.

Finally, do not assume every number with the right digits is valid. The factorization condition still has to hold exactly; a digit permutation alone is not enough.

Summary

  • A four-digit vampire number factors into two two-digit fangs using exactly the same digits.
  • The two fangs cannot both end in zero.
  • A direct search over two-digit factor pairs is simple and fast for this problem.
  • Comparing sorted digit strings is an easy correctness check.
  • The four-digit vampire numbers are 1260, 1395, 1435, 1530, 1827, 2187, and 6880.
  • Starting the factor loop at b = a avoids duplicate fang pairs.

Course illustration
Course illustration

All Rights Reserved.