%I #61 Aug 27 2022 04:25:15
%S 0,1,1,1,1,1,1,1,1,1,2,1,2,6,4,5,3,3,5,4,6,2,1,2,6,4,5,3,3,5,4,6,2,1,
%T 2,6,4,5,3,3,5,4,6,2,1,2,6,4,5,3,3,5,4,6,2,1,2,6,4,5,3,3,5,4,6,2,1,2,
%U 6,4,5,3,3,5,4,6,2,1,2,6,4,5,3,3,5,4,6
%N Length of the preperiodic part of 'Roll and Subtract' trajectory of n.
%C 'Roll and Subtract' is defined by x -> |x - roll(x)|, where roll(x) takes the first digit of a number and moves it to the back (rolls it around to the back).
%C Differs from A151962 first at n=101. - _R. J. Mathar_, May 07 2021
%H Jonathon Priestley, <a href="/A343383/b343383.txt">Table of n, a(n) for n = 0..9999</a>
%e a(119) = 4 since |119 - 191| = 72 -> |72 - 27| = 45 -> |45 - 54| = 9 -> |9 - 9| = 0. The value a(0) maps to 0, so the sequence ends there after 4 values have been traversed.
%e a(12737) = 1 since |12737 - 27371| = 14634 -> |14634 - 46341| = 31707 -> |31707 - 17073| = 14634. Since 14634 is already in the sequence, the sequence ends there.
%t Array[Function[w, LengthWhile[w, # != Last[w] &]]@ NestWhileList[Abs[# - FromDigits@ RotateLeft@ IntegerDigits[#]] &, #, Unequal, All] &, 105, 0] (* _Michael De Vlieger_, Apr 13 2021 *)
%o (Python)
%o def roll(n):
%o """ Moves first digit to the back """
%o s = str(n)
%o return int(s[1:] + s[0])
%o def backtrack(past, length, offset, dct):
%o """ Goes through every value passed and adds it and it's length to the dictionary """
%o if length == 0:
%o for elem in past:
%o dct[elem] = 0
%o i = 0
%o while length > 0:
%o n = past[i]
%o dct[n] = length + offset
%o i += 1
%o length -= 1
%o return dct
%o def a(n, dct):
%o past = []
%o length = 0
%o while (n not in dct):
%o past.append(n)
%o length += 1
%o n = abs(n - roll(n))
%o if n in past: # For duplicates
%o length = past.index(n)
%o dct = backtrack(past, length, 0, dct)
%o return dct, length
%o offset = dct[n]
%o dct = backtrack(past, length, offset, dct)
%o length += offset
%o return dct, length
%o dct = {}
%o sequence = []
%o i = 1
%o while i < 1000:
%o out = a(i, dct)
%o dct = out[0]
%o sequence.append(out[1])
%o i += 1
%Y Cf. A072137 (reverse and subtract).
%K nonn,base
%O 0,11
%A _Jonathon Priestley_, Apr 12 2021