Define an m∈N to be good if f(m)=m for all such f. It will be shown that m is good if and only if m has two or more distinct prime divisors. Let P(x,y) denote the assertion
gcd(f(f(x)),f(x+y))=gcd(x,y)
for a pair x,y∈N. Let x be a positive integer with two or more distinct prime divisors and let pk be largest power of one of these prime divisors such that pk∣x. If x=pk⋅q, then pk and q are relatively prime and x>pk,q>1. By P(q,x−q),
gcd(f(f(q)),f(x−q+q))=gcd(f(f(q)),f(x))=gcd(q,x−q)=q
which implies that q∣f(x). By P(pk,x−pk),
gcd(f(f(pk)),f(x−pk+pk))=gcd(f(f(pk)),f(x))=gcd(pk,x−pk)=pk
which implies that pk∣f(x). Since pk and q are relatively prime, x=pk⋅q divides f(x), which implies that f(x)≥x. Now assume for contradiction that f(x)>x. Let y=f(x)−x>0 and note that, by P(x,y), it follows that
f(f(x))=gcd(f(f(x)),f(x+f(x)−x))=gcd(x,f(x)−x)=gcd(x,f(x)).
Therefore f(f(x))∣x and f(f(x))∣f(x). By P(x,x), it follows that
gcd(f(f(x)),f(2x))=gcd(x,x)=x
This implies that x∣f(f(x)), which when combined with the above result, yields that f(f(x))=x. Since x∣f(x) and x is divisible by at least two distinct prime numbers, f(x) is also divisible by at least two distinct prime numbers. As shown previously, this implies that f(x)∣f(f(x))=x, which is a contradiction since f(x)>x. Therefore f(x)=x for all positive integers x with two or more distinct prime divisors.
Now it will be shown that all m∈N such that either m has one prime divisor or m=1 are not good. In either case, let m=pk where k≥0 and p is a prime number and consider the function satisfying that f(pk)=pk+1,f(pk+1)=pk and f(x)=x for all x=pk,pk+1. Note that this function also satisfies that f(f(x))=x for all positive integers x. If x+y=pk,pk+1, then P(x,y) holds by the Euclidean
algorithm since f(f((x))=x and f(x+y)=x+y. If x+y=pk+1, then P(x,y) is equivalent to gcd(x,pk)=gcd(x,pk+1−x)=gcd(x,pk+1) for all x<pk+1 which holds since the greatest power of p that can divide x is pk. If x+y=pk, then P(x,y) is equivalent to gcd(x,pk+1)=gcd(x,pk−x)=gcd(x,pk) for all x<pk which holds as shown above. Note that if m=1 then this case cannot occur. Since this function satisfies P(x,y),m is good if and only if m has two or more distinct prime divisors.