algorithms
pandigital numbers
computational efficiency
number theory
mathematics

Fastest algorithm to check if a number is pandigital?

Master System Design with Codemia

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

Introduction

Checking whether a number is pandigital looks simple, but performance depends on the exact definition and input size. In most coding tasks, pandigital means digits one through nine each appear exactly once, with no zero. This article compares practical approaches and focuses on fast, branch-light checks.

Define the Variant First

Before optimizing, pin down which variant you need:

  • One through nine pandigital means exactly nine digits, each of 1 to 9 once.
  • Zero through nine pandigital means exactly ten digits, each of 0 to 9 once.
  • Some tasks allow arbitrary digit sets and custom lengths.

The fastest algorithm for one variant may be wrong for another. For example, a mask target for one through nine differs from zero through nine.

Bitmask Method

A bitmask is usually the fastest general solution in high-level languages and native code. Each digit sets one bit. If a digit repeats, you detect it immediately. At the end, compare against expected mask.

python
1def is_pandigital_1_to_9(n: int) -> bool:
2    if n <= 0:
3        return False
4
5    mask = 0
6    digits = 0
7
8    while n:
9        d = n % 10
10        if d == 0:
11            return False
12
13        bit = 1 << d
14        if mask & bit:
15            return False  # repeated digit
16
17        mask |= bit
18        digits += 1
19        n //= 10
20
21    target = 0b1111111110  # bits 1..9 set
22    return digits == 9 and mask == target
23
24
25for x in [123456789, 987654321, 112345678, 102345678]:
26    print(x, is_pandigital_1_to_9(x))

Why it is fast:

  • Constant memory.
  • One pass over digits.
  • Early exit on invalid digit or repetition.
  • No sorting or heap allocations.

String and Set Baseline

A readable baseline uses strings and sets. It is concise but often slower due to allocation and hashing overhead.

python
def is_pandigital_set(n: int) -> bool:
    s = str(n)
    return len(s) == 9 and set(s) == set("123456789")

This can be acceptable for scripts, but for tight loops bitmask usually wins.

Micro-Optimizations for Hot Paths

If this check runs millions of times, you can improve further:

  • Reject by digit count first. For one through nine, valid range is from 123456789 to 987654321.
  • Use integer arithmetic only, avoiding conversion to string.
  • Keep branch order aligned with likely failures for early exits.
python
1def is_pandigital_1_to_9_fast(n: int) -> bool:
2    if n < 123456789 or n > 987654321:
3        return False
4
5    mask = 0
6    while n:
7        d = n % 10
8        bit = 1 << d
9        if d == 0 or (mask & bit):
10            return False
11        mask |= bit
12        n //= 10
13
14    return mask == 0b1111111110

In compiled languages, this pattern maps well to low instruction counts and predictable memory behavior.

Zero Through Nine Variant

For zero through nine, adjust the target and expected digit count.

python
1def is_pandigital_0_to_9(n: int) -> bool:
2    if n < 1023456789 or n > 9876543210:
3        return False
4
5    mask = 0
6    digits = 0
7
8    while n:
9        d = n % 10
10        bit = 1 << d
11        if mask & bit:
12            return False
13        mask |= bit
14        digits += 1
15        n //= 10
16
17    target = 0b1111111111  # bits 0..9 set
18    return digits == 10 and mask == target

Note that leading zero is impossible in integer representation, which is usually what you want.

Complexity

For both bitmask variants:

  • Time complexity is linear in digit count.
  • Space complexity is constant.

Since digit count is bounded by machine integer width, this is effectively very small constant-time work in many practical tasks.

Common Pitfalls

  • Forgetting to define which digits are required. Fix by documenting variant and tests before coding.
  • Accepting repeated digits because only final mask is checked. Fix by duplicate detection during scan.
  • Treating zero incorrectly in one-through-nine checks. Fix by immediate reject when digit zero appears.
  • Ignoring length constraint. Fix by validating digit count or numeric range.
  • Converting to string in performance-critical loops. Fix by using arithmetic extraction with modulo and integer division.

Summary

  • Bitmask digit scanning is the most practical fast method for pandigital checks.
  • Always define the pandigital variant first.
  • Detect duplicates during scanning for early exits.
  • Validate length or numeric range to avoid false positives.
  • String and set approaches are readable but usually slower for heavy workloads.

Course illustration
Course illustration

All Rights Reserved.