N=input()
f=lambda p,q:f(f(p,q-1),-~p%N)if p*q else q
print([i for i in range(1,N)if N%i+f(1,i)<1]+[N])[:N&N-1<1]
-2 bytes thanks to @Jakque
Try it online!
The short, recursive approach is slow since it repeats computation of many entries, so I've made an iterative, memoized approach which is much faster (quadratic in N
) and can compute up to N=4096
within 60 seconds on TIO.
How it Works
The algorithm assumes that \$\star\$ is unique, if it exists (proof below). It uses recursion, setting
\$p\star q=\begin{cases}
q, &p=0 \\
p+1 \mod n, &q=1 \\
(p \star (q-1 \mod n))\star (p+1 \mod n), &\text{otherwise}
\end{cases}\$
The first case (\$p=0\$) is Lemma 1 in the uniqueness proof: \$0\star q=q\$.
The second case (\$q=1\$) is given.
The third case has a simple proof despite its complex self (assuming addition and subtraction are mod \$N\$:
\begin{align*}
&p \star q\\
=&p \star ((q-1)+1) \\
=&p \star ((q-1)\star 1) \\
=&(p \star (q-1))\star(p\star 1) \\
=&(p \star (q-1))\star(p+1)
\end{align*}
This is enough to determine the unique \$\star\$ (The proof shows that the third case will not recurse indefinitely).
The period is the minimum \$i\$ such that \$1\star (i+1) = 1\star 1 = 2\$. If this \$i\$ doesn't divide \$N\$, then it is not a period.
Ungolfed code:
# f(p,q,N) = p⋆q
def f(p,q,N):
if p==0:
return q
if q==1:
return (p+1)%N
return f(f(p,(q-1)%N,N),(p+1)%N, N)
def period(N):
if any(f(p,f(q,r,N),N) != f(f(p,q,N),f(p,r,N),N) for p in range(N) for q in range(N) for r in range(N)):
# The f does not exist
return []
for i in range(1, N):
# period is i if f(1,i+1)=f(1,1)=2
if f(1,i+1,N) == 2:
# might not be divisible, in which case it is not the period
if N%i:
return [N]
return [i]
This could be sped up immensely by memoization utilizing \$ a \star 2 = a \star (1 \star 1) = (a\star 1)\star (a\star 1) = (a+1)\star(a+1) \$ and \$1 \star a = (1\star (a-1))\star 2\$.
Proof of Uniqueness of \$\star\$
For this section, we assume all addition/subtraction is mod \$N\$.
Lemma 1: For all \$a\$, \$0 \star a = a\$
Proof: Induction
Base case: \$a=1\$. Given since \$0\star 1=1\$.
Step: Assume this lemma holds for \$a=k\$. Then \begin{align*}
&0 \star (k + 1)\\
=& 0\star (k\star 1) \\
=& (0\star k)\star (0\star 1) \\
=& k \star 1 \\
=& k+1,
\end{align*}
so this holds for \$a=k+1\$. Since this addition is mod \$N\$, this will "wrap around" back to \$0\$, so it applies for \$a=0\$ too, not just \$a\geq 1\$
Theorem: If an operation \$\star\$ is valid for a given \$N\$, then it the unique \$\star\$ that is valid for that \$N\$.
Proof: We use strong induction to show that, for all \$0\leq a,b\leq N-1\$, \$a\star b\$ is uniquely determined by \$N\$. In addition, for each \$a\$ and \$b\$ in the same set, either (1) \$a\star b=0\$ or (2) \$a<a\star b<N\$
Base case: \$a\star b\$ is uniquely determined and equal to \$0\$ for \$a=N-1\$ and for all \$b\$.
We prove this base case with induction too.
- Base case: \$b=1\$. We are given \$a\star 1=a+1\$, so \$(N-1)\star 1=0\$, uniquely determined.
- Step: Given \$(N-1)\star b=0\$ for \$b=k\$, then
\begin{align*}
&(N-1)\star (b+1)\\
=&(N-1)\star (b\star 1)\\
=&((N-1)\star b)\star((N-1)\star 1)\\
=&0\star0 \\
=&0,
\end{align*}
where we use Lemma 1 to show \$0\star 0=0\$,
so \$(N-1)\star b\$ is uniquely determiend to be \$0\$.
Assume \$a\star b\$ is uniquely determined and either (1) or (2) hold for all \$k<a\leq N-1\$ and for all \$b\$. Then for \$a=k\$, we will show \$a\star b\$ is uniquely determined and either (1) or (2) hold for all \$b\$, again with induction.
Base case: \$k\star b\$ is uniquely determined and either (1) or (2) hold for \$b=1\$. This is trivial since \$k\star 1=k+1>k\$, so \$k\star 1\$ is unique and (2) holds
Step: Assume \$k\star b\$ is uniquely determined and either (1) or (2) hold for \$b=j\$. Then:
\begin{align*}
&k\star (b+1)\\
=&k\star (b\star 1)\\
=&(k\star b)\star(k\star 1)\\
=&(k\star b)\star(k+1)
\end{align*}
If (1) holds, then \$k\star b=0\$, so \$k\star(b+1)=k+1>k\$ by Lemma 1. Thus \$k\star(b+1)\$ is uniquely determined and satisfies (2).
If (2) holds, then \$k<k\star b\leq N-1\$,so by the outer inductive assumption, \$k\star(b+1)=(k\star b)\star (k+1)\$ is uniquely determined and either (1) or (2) holds for it.
In either case, \$k\star(b+1)\$ is uniquely determined and either (1) or (2) hold for it, completing the inductive step.
N=1..1024
, seperated by newlines. Later I decided to take input, and enlarge the upper limit. Forgot to change the tag. \$\endgroup\$N>4
? \$\endgroup\$n=2
be[1]
? The only possible operation has the table0 1 \\ 0 0
, and0 0
has period 1. \$\endgroup\$N
is a power of 2. \$\endgroup\$N
a power of 2? \$\endgroup\$