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
aandbmatches the digits ofn - '
aandbdo not both end in zero'
For example, 1260 qualifies because:
The digits match after sorting, so 1260 is a vampire number.
Brute Force Search
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.
This prints:
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:
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, and6880. - Starting the factor loop at
b = aavoids duplicate fang pairs.

