# What's the next base-ten non-pandigital factorial number after 41!?

By pandigital number I mean a number for which each digit in a given base occurs at least once (some definitions that state each digit must occur exactly once), and since I looking for numbers that are not pandigital in base ten at least one of the digits from 0 through 9 should be missing. By a factorial number I mean a positive integer for which there exists a whole number $$n$$ such that the factorial number is equal to $$n!$$. In set theoretic language, this question is considering elements in the intersection of these two sets of numbers.

One can quickly generate and test (brute force) search for such values. Here's a quick-and-simple example of such an algorithm.

from math import factorial

n = 0

while 1:
f = factorial(n)
if len(set(str(f))) != 10:
print(n, f)
n += 1


Which running this for even a few seconds will print the following before not printing anything after. For the $$n$$ as I use it in the definitions above (which is consistent with the Python script), I have exhaustively checked for values of $$n$$ from 0 to over 253817 without finding what the next non-pandigital factorial number is. I've spoken to a number theorist about this problem, and while he told me there is an infinite number of such numbers, he did not have an example of one higher than 41!. Note that in this question, I don't just want a higher non-pandigital factorial number, but the next one.

0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
11 39916800
12 479001600
13 6227020800
14 87178291200
15 1307674368000
16 20922789888000
17 355687428096000
18 6402373705728000
19 121645100408832000
20 2432902008176640000
21 51090942171709440000
22 1124000727777607680000
24 620448401733239439360000
25 15511210043330985984000000
26 403291461126605635584000000
28 304888344611713860501504000000
29 8841761993739701954543616000000
30 265252859812191058636308480000000
32 263130836933693530167218012160000000
38 523022617466601111760007224100074291200000000
41 33452526613163807108170062053440751665152000000000


Note that the last number of $$n$$ above that satisfies the criterion is 41, not 42. Douglas Adams will mock me for coming up short by unity!

Here is a Rust implementation to continue the calculation.

extern crate rug;

use rug::Integer;

fn main() {
let mut n = Integer::from(0);

loop {
let f = factorial(&n);
if count_unique_digits(&f.to_string()) != 10 {
println!("{} {}", n, f);
}
n += 1;
}
}

fn factorial(n: &Integer) -> Integer {
if n == &Integer::from(0) {
return Integer::from(1);
}

let mut result = Integer::from(1);
for i in 1..=n.to_u64().unwrap() {
result *= &Integer::from(i);
}
result
}

fn count_unique_digits(num_str: &str) -> usize {
let mut digits = std::collections::HashSet::new();
for ch in num_str.chars() {
if ch.is_digit(10) {
digits.insert(ch);
}
}
digits.len()
}

• I think there's a good chance that you misunderstood your number theorist, or he misunderstood you. It seems very likely to me that $41!$ is the largest non-pandigital factorial. (If you replace every factorial number with a random number with the same number of digits and the same number of trailing zeroes, then on average you'll get finitely many non-pandigital numbers, and the expected number you'll get for $n>41$ is a lot less than $1$. If there really are infinitely many non-pandigital factorials, then there's something really abnormal about the distribution of digits in factorials.) Commented Apr 7, 2020 at 4:27
• @Micah That's certainly possible. If you rigorously and formally prove that there actually isn't a next non-pandigital factorial number, I will accept that as an answer. I'm willing to learn new concepts, but note that I am a layperson so I will likely have to have trivial things that should be left as an exercise for the reader explicitly shown to me. Commented Apr 7, 2020 at 4:32
• If I had a rigorous proof, I would have written it as an answer. In general, properties of digits like this are easy to conjecture and essentially impossible to prove. Commented Apr 7, 2020 at 13:45
• @Micah I have decided to start calculating again, at least until my computer system can't handle the resource requirements. Commented Apr 8, 2020 at 4:03
• The notes in this OEIS sequence include a conjecture that $41$ is the last term. Commented Apr 12, 2020 at 4:47

According to Stirling’s approximation, $$n!$$ has

$$\log_{10}n!\approx\log_{10}\left(\sqrt{2\pi n}\left(\frac n{\mathrm e}\right)^n\right)=\frac12\log_{10}(2\pi n)+n\log_{10}\left(\frac n{\mathrm e}\right)$$

decimal digits. Since $$\log_{10}n$$ varies slowly, we can approximate it by $$\log_{10}42$$. Thus,

$$\log_{10}n!\lesssim1.21+1.19n\;.$$

There is a trailing zero for each factor of $$5$$, and there are about $$n\left(\frac15+\frac1{25}+\cdots\right)=\frac n4$$ factors of $$5$$ in $$n!$$. The first non-zero digit is known to be even, but that doesn’t change the probability for the number to be pandigital. So we can treat the remaining approximately $$1.12+0.94n$$ digits as independently uniformly random and find the probability that at least one of the nine non-zero digits is not present. By inclusion–exclusion, this is

$$\sum_{k=1}^9(-1)^{k+1}\binom9k\left(1-\frac k{10}\right)^{1.12+0.94n}\;.$$

At $$n=42$$, this is already only about $$0.12$$. Summing over $$n$$ yields the expected number of non-pandigital factorials beyond some $$n_0$$:

$$\sum_{n=n_0}^\infty\sum_{k=1}^9(-1)^{k+1}\binom9k\left(1-\frac k{10}\right)^{1.12+0.94n}=\sum_{k=1}^9(-1)^{k+1}\binom9k\frac{\left(1-\frac k{10}\right)^{1.12+0.94n_0}}{1-\left(1-\frac k{10}\right)^{0.94}}\;.$$

For $$n_0=42$$, this is about $$1.3$$. So we might have expected one more non-pandigital factorial beyond $$42!$$, but it’s not too much of a coincidence that there turned out to be none. You’ve checked up to $$n_0=117583$$; the expected number of non-pandigital factorials beyond that is about $$3\cdot10^{-5056}$$, so you can safely abort your search. (Remember that this is calculated with $$\log_{10}n\approx\log_{10}42$$, so the value is actually even lower.)

• @OscarLanzi: Very interesting. I had only thought about this superficially and decided that they were uniform. Your argument sounds convincing. But I just coded this and the penultimate non-zero digit does seem to be uniformly distributed. Here's the distribution up to $20000!$: $1959,2080,2062,1969,1985,1978,1986,1961,2020,1991$. (The values up to $10!$ aren't counted.) It looks like random fluctuations, and certainly nowhere near what it should be for a bias of $3:2$. We'll have to think about why that is :-) Commented Apr 19, 2020 at 21:26
• @OscarLanzi: Aha, it's actually quite simple, it just occurred to me in the shower :-). The bias is due to the residues modulo $10^n$ that end in zeros (e.g. $00,20,40,60,80$ for $n=2$ for the penultimate digit), and these don't occur because we're only considering the non-zero digits. Commented Apr 19, 2020 at 21:39
• @OscarLanzi: I think you'd actually have a better point at the other end of the string :-). The first digit is distributed according to Benford's law, and the next digits are also affected by this scaling law – e.g. for the second digit, $0$ is still about $1.5$ times more frequent than $9$. Commented Apr 19, 2020 at 21:43