Let N={1,2,…,n−1}. For a,b∈N, we say that b follows a if there exists an integer g such that b≡ga(modn) and denote this property as a→b. This way we have a directed graph with N as set of vertices. If a1,…,an−1 is a permutation of 1,2,…,n−1 such that a1→a2→…→an−1→a1 then this is a Hamiltonian cycle in the graph.
Step I. First consider the case when n is composite. Let n=p1α1…psαs be its prime factorization. All primes pi are odd.
Suppose that αi>1 for some i. For all integers a,g with a≥2, we have ga≡pi(modpi2), because ga is either divisible by pi2 or it is not divisible by pi. It follows that in any Hamiltonian cycle pi comes immediately after 1. The same argument shows that 2pi also should come immediately after 1, which is impossible. Hence, there is no Hamiltonian cycle in the graph.
Now suppose that n is square-free. We have n=p1p2…ps>9 and s≥2. Assume that there exists a Hamiltonian cycle. There are 2n−1 even numbers in this cycle, and each number which follows one of them should be a quadratic residue modulo n. So, there should be at least 2n−1 nonzero quadratic residues modulo n. On the other hand, for each pi there exist exactly 2pi+1 quadratic residues modulo pi; by the Chinese Remainder Theorem, the number of quadratic residues modulo n is exactly 2p1+1⋅2p2+1⋅…⋅2ps+1, including 0. Then we have a contradiction by
2p1+1⋅2p2+1⋅…⋅2ps+1≤32p1⋅32p2⋅…⋅32ps=(32)sn≤94n<2n−1.
This proves the "if"-part of the problem.
Step II. Now suppose that n is prime. For any a∈N, denote by ν2(a) the exponent of 2 in the prime factorization of a, and let μ(a)=max{t∈[0,k]∣2t→a}.
Lemma. For any a,b∈N, we have a→b if and only if ν2(a)≤μ(b).
Proof. Let ℓ=ν2(a) and m=μ(b).
Suppose ℓ≤m. Since b follows 2m, there exists some g0 such that b≡g02m(modn). By gcd(a,n−1)=2ℓ there exist some integers p and q such that pa−q(n−1)=2ℓ. Choosing g=g02m−ℓp we have ga=g02m−ℓpa=g02m+2m−ℓq(n−1)≡g02m≡b(modn) by Fermat's theorem. Hence, a→b.
To prove the reverse statement, suppose that a→b, so b≡ga(modn) with some g. Then b≡(ga/2ℓ)2ℓ, and therefore 2ℓ→b. By the definition of μ(b), we have μ(b)≥ℓ. The lemma is proved.
Now for every i with 0≤i≤k, let
AiBiand Ci={a∈N∣ν2(a)=i},={a∈N∣μ(a)=i},={a∈N∣μ(a)≥i}=Bi∪Bi+1∪…∪Bk.
We claim that ∣Ai∣=∣Bi∣ for all 0≤i≤k. Obviously we have ∣Ai∣=2k−i−1 for all i=0,…,k−1, and ∣Ak∣=1. Now we determine ∣Ci∣. We have ∣C0∣=n−1 and by Fermat's theorem we also have Ck={1}, so ∣Ck∣=1. Next, notice that Ci+1={x2modn∣x∈Ci}. For every a∈N, the relation x2≡a(modn) has at most two solutions in N. Therefore we have 2∣Ci+1∣≤∣Ci∣, with the equality achieved only if for every y∈Ci+1, there exist distinct elements x,x′∈Ci such that x2≡x′2≡y(modn) (this implies x+x′=n). Now, since 2k∣Ck∣=∣C0∣, we obtain that this equality should be achieved in each step. Hence ∣Ci∣=2k−i for 0≤i≤k, and therefore ∣Bi∣=2k−i−1 for 0≤i≤k−1 and ∣Bk∣=1.
From the previous arguments we can see that for each z∈Ci(0≤i<k) the equation x2≡z2(modn) has two solutions in Ci, so we have n−z∈Ci. Hence, for each i=0,1,…,k−1, exactly half of the elements of Ci are odd. The same statement is valid for Bi=Ci\Ci+1 for 0≤i≤k−2. In particular, each such Bi contains an odd number. Note that Bk={1} also contains an odd number, and Bk−1={2k} since Ck−1 consists of the two square roots of 1 modulo n.
Step III. Now we construct a Hamiltonian cycle in the graph. First, for each i with 0≤i≤k, connect the elements of Ai to the elements of Bi by means of an arbitrary bijection. After performing this for every i, we obtain a subgraph with all vertices having in-degree 1 and outdegree 1, so the subgraph is a disjoint union of cycles. If there is a unique cycle, we are done. Otherwise, we modify the subgraph in such a way that the previous property is preserved and the number of cycles decreases; after a finite number of steps we arrive at a single cycle.
For every cycle C, let λ(C)=minc∈Cν2(c). Consider a cycle C for which λ(C) is maximal. If λ(C)=0, then for any other cycle C′ we have λ(C′)=0. Take two arbitrary vertices a∈C and a′∈C′ such that ν2(a)=ν2(a′)=0; let their direct successors be b and b′, respectively. Then we can unify C and C′ to a single cycle by replacing the edges a→b and a′→b′ by a→b′ and a′→b.
Now suppose that λ=λ(C)≥1; let a∈C∩Aλ. If there exists some a′∈Aλ\C, then a′ lies in another cycle C′ and we can merge the two cycles in exactly the same way as above. So, the only remaining case is Aλ⊂C. Since the edges from Aλ lead to Bλ, we get also Bλ⊂C. If λ=k−1 then Bλ contains an odd number; this contradicts the assumption λ(C)>0. Finally, if λ=k−1, then C contains 2k−1 which is the only element of Ak−1. Since Bk−1={2k}=Ak and Bk={1}, the cycle C contains the path 2k−1→2k→1 and it contains an odd number again. This completes the proof of the "only if"-part of the problem.