Accidental divisibility sequence? It seems (verified for $n \leq 3 \cdot 10^5$) that when the equation

$$p = n!+1 \bmod{\frac{n^2(n+1)^2}{4}}$$

is satisfied with some $n&gt;1\in\mathbb N$ and $p\in\mathbb P$, it implies $n\in\mathbb P$ as well. The overall feel reminds me of a divisibility sequence like Fibonacci or $2^n-1$, which is the only time I've seen prime values locked to prime indices like that. However, I don't see many other factors being passed along with index factors, which is what I would normally expect.

Note that this applies only to the specific $p$ calculated as above using mod as an operation, and not to any arbitrary $p$ satisfying a congruence.

Can anyone explain why my sequence is behaving this way?



Upon reflection I think it has to do with my using Wilson's Theorem in a mangled way, and consequently I'm only getting non-$1$ values near primes and $2p$ semiprimes.

Some Mathematica code to show a table if you like:



upperBound = 100;
pretty[n_] := 
  If[Length@# &gt; 1, CenterDot @@ #, First@#] &amp;@
    FactorInteger[n] /. {{b_, 1} :&gt; b, {b_, e_} :&gt; Superscript[b, e]};
boldPrime[n_] := If[IntegerQ[n] &amp;&amp; PrimeQ[n], Style[n, Bold, Blue], n];
Grid[Table[{n, boldPrime@pretty@n, 
   boldPrime@pretty@Mod[n! + 1, Plus @@ (n^2 (n + 1)^2/4)]}, {n, 2, 
   upperBound}], Alignment -&gt; Right]




My updated thoughts:

For all $n&gt;9$, it seems like $p=1$ except in one of three conditions:


$n+1$ is prime, in which case $n+1$ divides $p$
$n+1$ is an even semiprime, i.e. $2q$ with prime $q$, and in which case $2$ divides $p$
$n$ is prime, in which case $p$ may or may not be prime, presumably randomly.


If these are indeed the conditions (and I've verified them through $10^5$, so it seems very likely), that would be sufficient to explain the primality correlation. However, I'm still looking for a cogent explanation for why these particular conditions arise from my equation, and will happily accept an answer that can give me some insight into that.
You have $$41617=8!+1+\frac{8^2(8+1)^2}{4}$$
Yet 41617 is a prime and 8 isn't.

There are no smaller counterexamples as $4!+1+k\frac{4^2(4+1)^2}{4}$ will always be a multiple of $5$ (for any $k$), and $6!+1+k\frac{6^2(6+1)^2}{4}$ will always be a multiple of $7$.
Let $n=9,p=6481.$ $p$ is prime, while $n$ is composite, and

$$6481\equiv 9!+1\pmod{\frac{9^2(9+1)^2}4}$$