%I #20 Mar 14 2021 14:30:30
%S 5,7,9,2,11,3,13,4,17,6,19,8,23,22,21,10,25,12,27,16,15,14,29,18,31,
%T 20,33,24,37,26,35,28,39,32,41,30,43,34,47,36,53,38,49,52,51,40,55,44,
%U 57,46,45,42,59,48,61,50,63,54,67,56,65,58,69,62,71,60,73,64,79,66,77,68,81,82,83,72,85,70,87,74,75,76,89,78,91,80,93,84,97,86,95,88,99,92,101
%N a(n) is the smallest number larger than 1, coprime to n, and differing from n by at least 2, that has not yet appeared in the sequence.
%C It has been proved that a(n) is its own inverse, i.e., that a(a(n)) = n for all n > 1.
%H Robert Israel, <a href="/A290151/b290151.txt">Table of n, a(n) for n = 2..10000</a>
%H Robert Israel, <a href="https://math.stackexchange.com/a/2366358/276060">Proof that a(n) is its own inverse</a>, Mathematics Stack Exchange
%e a(2), must be at least 4 to differ by 2, but 4 and 2 share a factor of 2 so a(2) must be 5.
%e a(3), must be at least 5 but 5 is taken by a(2), so it is at least 6, but 6 shares a factor with 3 so it must be at least 7, 7 satisfies all three requirements thus a(3)=7.
%p S:= [$2..1000]:
%p while S <> {} do
%p n:= S[1];
%p k:= ListTools:-SelectFirst(t -> S[t] > n+1 and igcd(n, S[t])=1, [$2..nops(S)]);
%p if k = NULL then break fi;
%p R[n]:= S[k]; R[S[k]]:= n;
%p S:= subsop(1=NULL,k=NULL,S);
%p od:
%p seq(R[i],i=2..n-1); # _Robert Israel_, Jul 25 2017
%t a = {1, 5}; Do[k = 2; While[Nand[CoprimeQ[n, k], ! MemberQ[a, k], Abs[n - k] >= 2], k++]; AppendTo[a, k], {n, 3, 96}]; Rest@ a (* _Michael De Vlieger_, Jul 21 2017 *)
%o (Haskell)
%o f 1=[]
%o f n|l<-f$n-1=l++[[i|i<-[2..],gcd i n<2,all(/=i)l,abs(n-i)>1]!!0]
%o -- defines a function f that when given n will generate the first n terms as a list --
%K nonn
%O 2,1
%A _Eamon Olive_, Jul 21 2017
|