Fermat's little theorem states that for $n$ prime,

$$ a^n \equiv a \pmod{n}. $$

The values of $n$ for which this holds are the primes and the Carmichael numbers. If we modify the congruence slightly,

$$ a^{n - 1} \equiv a \pmod{n}, $$

the values of $n$ for which it holds are listed in A216090: 1, 2, 6, 10, 14, 22, 26, 30, 34, 38, 46, ...

More generally, we can examine the values of $n$ where

$$ a^{n - d} \equiv a \pmod{n} $$

for all $a$ and a given $d$. $d = 0$ gives the primes/Carmichael numbers, $d = 1$ gives the sequence A216090, and $d = 2$ gives the sequence 1, 2, 3, 15, 21, 33, 39, 51, 57, 69, 87, ...

For $d = 3$, the sequence grows much more rapidly: 1, 2, 4, 6, 154, 4774, 23254, 179014, 187054, 1066054, 1168654, ... But for $d = 4$, the growth slows down again: 1, 2, 3, 5, 65, 85, 145, 165, 185, 205, 265, ...

The first few $d$ values which give fast-growing sequences for $n$ are 3, 7, 8, 11, 15, 17, 19, 23, 24, and 26. In general, the sequence seems to grow particularly quickly when $d + 1$ is not squarefree.

I've proven this generalization of Korselt's criterion that characterises the possible $n$ values for a given $d$:

**Theorem** (Generalized Korselt's criterion). Let $d$ be an arbitrary integer, and $n$ a positive integer such that and $n \ge d + 1$. Then $a^{n - d} \equiv a \pmod{n}$ for all $a \in \mathbb{Z}$ if and only if either

- $n = 1$ or $2$;
- $n = d + 1$; or
- $n$ is squarefree, and for each of $n$'s prime divisors $p_i$, $(p_i - 1) \vert (n - d - 1)$.

Additionally, I've observed the following pattern that would explain why the squarefreeness of $d + 1$ matters:

**Conjecture**. When $d + 1$ is squarefree, many prime multiples of $d + 1$ are valid values for $n$. But if $d + 1$ is not squarefree, no multiples of it (except possibly itself) are feasible values for $n$. In either case, valid $n$ values which are not multiples of $d + 1$ are much less abundant.

Any ideas how to tackle this conjecture, or at least make it more explicit?