Solution:
a.
Let g be a positive integer with the given property. So for each odd prime number p there exists a positive integer n such that p∣gn−n and p∣gn+1−(n+1).
If g has an odd prime factor p, then from p∣gn−n it follows that p∣n, while from p∣gn+1−(n+1) we deduce that p∣n+1. But p cannot divide both n and n+1; contradiction. So g is a power of 2: g=2k for some k⩾0.
If g=20=1, then p∣1−n and p∣1−(n+1), which is again a contradiction.
Suppose k⩾2. Then g−1 has an odd prime factor p, therefore g≡1(modp) so 0≡gn−n≡1−n(modp) and 0≡gn+1−(n+1)≡1−(n+1)(modp), which is again a contradiction.
Now we prove that g=21=2 does satisfy the condition. Let a prime p>2 be given. Choose n=(p−1)2, then we have n≡(−1)2=1(modp). By Fermat's little theorem (using gcd(2,p)=1) we know that 2p−1≡1(modp), so
2n=2(p−1)2=(2p−1)p−1≡1≡n(p)
Multiplying both sides by 2, we see that also
2n+1≡2n=n+n≡n+1(p)
We conclude that only g=2 has the given property.
b.
Let g be a positive integer with the given property. So for each odd prime number p there exists a positive integer n such that p∣gn−n2 and p∣gn+1−(n+1)2.
If g has an odd prime factor p, then from p∣gn−n2 it follows that p∣n2, so also p∣n, while from p∣gn+1−(n+1)2 we deduce that p∣(n+1)2, so also p∣n+1.
But p cannot divide both n and n+1; contradiction. So g is a power of 2: g=2k for some k⩾0.
If g=20=1, then for any odd prime p we have p∣1−n2=(1−n)(1+n) and p∣1−(n+1)2=(1−(n+1))(1+(n+1)). Now take p=5. The first statement says that n≡1 or n≡−1≡4(mod5), and the second that n≡0 or n≡−2≡3 (mod5). But this yields a contradiction.
If g=21=2, then for any odd prime p we have p∣2n−n2 and p∣2n+1−(n+1)2. Now take p=3. As 3∤2n and 3∤2n+1, we know that 3∤n2 and 3∤(n+1)2. So these two squares must be 1 modulo 3 (as 2 can never be a square modulo 3). Therefore also 2n and 2n+1 must be 1 modulo 3, which gives 2⋅1≡2⋅2n=2n+1≡1(mod3); contradiction.
Now suppose k⩾2. Then g−1 has an odd prime factor p, therefore g≡1(modp) so 0≡gn−n2≡1−n2=(1−n)(1+n)(modp) and 0≡gn+1−(n+1)2≡1−(n+1)2=(1−(n+1))(1+(n+1))(modp). Suppose p⩾5. The first statement says that n≡1 or n≡−1(modp), and the second that n≡0 or n≡−2(modp). But n can only be congruent to at most one of the numbers −2,−1,0 and 1, since p⩾5; contradiction. We conclude that p=3, so g−1 contains only prime factors 3. Hence 2k−1=3ℓ for some ℓ>0. We see that 2k−1≡(−1)k−1(mod3), while 3ℓ≡0(mod3). So k has to be even, say k=2m, and our equation becomes 22m−1=3ℓ, or equivalently (2m−1)(2m+1)=3ℓ. Not both factors on the left-hand side can be divisible by 3, so 2m−1=1 and 2m+1=3ℓ, so m=1. Hence g=22=4.
Now we show that g=4 does have the given property. For this we use that g=2 is a solution to part (a): for any odd prime p there exists a positive integer n such that
n≡2n(modp) and n+1≡2n+1(modp).
Taking the square of both congruences, we obtain
n2≡(2n)2=(22)n=4n(modp)
and
(n+1)2≡(2n+1)2=(22)n+1=4n+1(modp)
as desired.
We conclude that only g=4 has the given property.