%I #35 Jul 26 2020 17:32:20
%S 1,3,0,-4,1,7,0,-8,1,11,0,-12,1,15,0,-16,1,19,0,-20,1,23,0,-24,1,27,0,
%T -28,1,31,0,-32,1,35,0,-36,1,39,0,-40,1,43,0,-44,1,47,0,-48,1,51,0,
%U -52,1,55,0
%N a(1) = 1 and then add n to the previous term if n is coprime to the previous term, otherwise subtract n from the previous term. a(1) = 1 and a(n) = a(n-1) + n if gcd(n, a(n-1)) = 1, otherwise a(n) = a(n-1) - n.
%C a(2k+1) = (k+1) (mod 2), a(4k) = -4k, a(4k+2) = 4k+3. Proof: If a(4k+3)=0 then a(4k+4) = -4k-4, a(4k+5)=1, a(4k+6) = 1+4k+6 and again, a(4k+7)=0. - _Ralf Stephan_, Mar 18 2003
%C Abs(a(n)) = A003815(n). - _Reinhard Zumkeller_, Oct 09 2007
%C With different signs, it can be obtained as abs(a(n)) = abs(Sum_{i=0..n} (-1)^h(i)*i) where h(i) is the Hamming weight of i, A000120, the number of 1s in base 2. - _Olivier GĂ©rard_, Jul 30 2012
%H Vincenzo Librandi, <a href="/A077140/b077140.txt">Table of n, a(n) for n = 1..1000</a>
%F a(1) = 1 and a(n) = a(n-1) + n if gcd(n, a(n-1)) = 1, otherwise a(n) = a(n-1) - n.
%F G.f.: x(x^2-2x-1)/((x^2+1)^2*(x-1)). - _Ralf Stephan_, Mar 18 2003
%F Abs(a(n)) = ((n+1) mod 2)*n + (floor((n+(n mod 2))/2) mod 2). - Tj Wrenn (tjwrenn(AT)cs.utexas.edu), Apr 07 2005
%F a(n) = Sum_{k = 1..n} (-(-1)^((2*k - (-1)^k + 1)/4)*k). - _Ilya Gutkovskiy_, Dec 21 2015
%e From _Ilya Gutkovskiy_, Dec 21 2015: (Start)
%e a(1) = 1;
%e a(2) = 1 + 2 = 3;
%e a(3) = 1 + 2 - 3 = 0;
%e a(4) = 1 + 2 - 3 - 4 = -4;
%e a(5) = 1 + 2 - 3 - 4 + 5 = 1;
%e a(6) = 1 + 2 - 3 - 4 + 5 + 6 = 7;
%e a(7) = 1 + 2 - 3 - 4 + 5 + 6 - 7 = 0;
%e a(8) = 1 + 2 - 3 - 4 + 5 + 6 - 7 - 8 = -8; etc. (End)
%t CoefficientList[Series[(x^2-2x-1)/((x^2+1)^2*(x-1)),{x,0,100}],x] (* _Vincenzo Librandi_, Jul 30 2012 *)
%t nxt[{n_,a_}]:={n+1,If[CoprimeQ[a,n+1],a+n+1,a-n-1]}; NestList[nxt,{1,1},60][[All,2]] (* _Harvey P. Dale_, Jul 26 2020 *)
%o (PARI) v=vector(100);v[1]=1;for(k=2,100,if(gcd(v[k-1],k)>1,v[k]=v[k-1]-k,v[k]=v[k-1]+k));print(v)
%Y Cf. A077141.
%K sign,easy
%O 1,2
%A _Amarnath Murthy_, Oct 30 2002
%E More terms from _Ralf Stephan_, Mar 18 2003