%I #96 Oct 18 2023 02:11:15
%S 0,1,3,0,4,9,3,10,2,11,1,12,0,13,27,12,28,11,29,10,30,9,31,8,32,7,33,
%T 6,34,5,35,4,36,3,37,2,38,1,39,0,40,81,39,82,38,83,37,84,36,85,35,86,
%U 34,87,33,88,32,89,31,90,30,91,29,92,28,93,27,94,26,95,25,96,24,97,23,98
%N a(1)=0; thereafter a(n+1) = a(n) - n if a(n) >= n otherwise a(n+1) = a(n) + n.
%C p^a(n) = A084110(p^(n-1)) for n>1 and p prime. - _Reinhard Zumkeller_, May 12 2003
%C For n > 1: a(A029858(n)) = A029858(n) and a(A003462(n)) = 0. - _Reinhard Zumkeller_, May 09 2012
%C Absolute first differences of A085059; abs(a(n+1)-a(n)) = n, see also A086283. - _Reinhard Zumkeller_, Oct 17 2014
%C For n>3, when a(n) = 3, a(n+1) is in A116970. - _Bill McEachen_, Oct 03 2023
%H N. J. A. Sloane, <a href="/A008344/b008344.txt">Table of n, a(n) for n = 1..29524</a> (Up to the 10th 0 term)
%H <a href="/index/Rea#Recaman">Index entries for sequences related to Recamán's sequence</a>
%F This is a concatenation S_0, S_1, S_2, ... where S_i = [b_0, b_1, ..., b_{3^(i+1)-1}] with b_0 = 0, b_{2j-1} = k+1-j, b_{2j} = 2k+j (j=1..k), k=(3^(i+1)-1)/2. E.g. S_0 = [0, 1, 3], S_1 = [0, 4, 9, 3, 10, 2, 11, 1, 12].
%F a((3^n-1)/2) = 0; a((3^n-1)/2 + 2k-1) = (3^n+1)/2 - k for 1 <= k <= (3^n-1)/2; a((3^n-1)/2 + 2k) = 3^n - 1 + k for 1 <= k < (3^n-1)/2. - _Benoit Cloitre_, Jan 09 2003 [Corrected by _Jianing Song_, May 25 2021]
%F a(n) = (n-1+a(n-1)) mod (2*(n-1)). - _Jon Maiga_, Jul 09 2021
%p A008344 := proc(n) option remember; if n = 0 then 0 elif A008344(n-1) >= (n-1) then A008344(n-1)-(n-1) else A008344(n-1)+(n-1); fi; end;
%t a[1]=0; a[n_] := a[n]=If[a[n-1]>=n-1, a[n-1]-n+1, a[n-1]+n-1]
%t Transpose[ NestList[ If[First[#]>=Last[#],{First[#]-Last[#],Last[#]+1}, {First[#]+Last[#],Last[#]+1}]&,{0,1},80]][[1]] (* _Harvey P. Dale_, Jun 20 2011 *)
%t s = 0; Table[If[s < n, s = s + n, s = s - n], {n, 0, 80}] (* _Horst H. Manninger_, Dec 03 2018 *)
%o (Haskell)
%o a008344 n = a008344_list !! (n-1)
%o a008344_list = 0 : f 0 [1..] where
%o f x (z:zs) = y : f y zs where y = if x < z then x + z else x - z
%o -- _Reinhard Zumkeller_, Oct 17 2014, May 08 2012
%o (PARI) a(n) = my(expo = logint(2*n+1, 3), res = n - (3^expo-1)/2); if(res==0, 0, if(res%2, (3^expo-res)/2, 3^expo-1+res/2)) \\ _Jianing Song_, May 25 2021
%Y Cf. A046901, A008343, A088230, A085059, A086283.
%Y Equals A085059(n)-1.
%Y Cf. A076042 (based on squares).
%K nonn,easy,nice,look
%O 1,3
%A _N. J. A. Sloane_
%E Name edited by _Dmitry Kamenetsky_, Feb 14 2017