%I #10 Mar 16 2015 23:16:48
%S 1,2,2,2,2,2,2,2,2,2,3,2,3,7,5,6,4,4,6,5,7,3,2,3,7,5,6,4,4,6,5,7,3,2,
%T 3,7,5,6,4,4,6,5,7,3,2,3,7,5,6,4,4,6,5,7,3,2,3,7,5,6,4,4,6,5,7,3,2,3,
%U 7,5,6,4,4,6,5,7,3,2,3,7,5,6,4,4,6,5,7,3,2,3,7,5,6,4,4,6,5,7,3
%N (Length of preperiodic part) + (length of cycle) of trajectory of n under iteration of the Kaprekar map in A151949.
%C Equals A151962(n) + 1 iff n < 10001 (when a cycle of length greater than 1 occurs for the first time).
%H Joseph Myers and Robert G. Wilson v, <a href="/A151963/b151963.txt">Table of n, a(n) for n = 0..1000 </a>.
%H <a href="/index/K#Kaprekar_map">Index entries for the Kaprekar map</a>
%e 13->18->63->27->45->9->0->0, so a(13)=6+1 = 7.
%p # Maple program from _R. J. Mathar_:
%p A151949 := proc(n)
%p local tup;
%p tup := sort(convert(n,base,10)) ;
%p add( (op(i,tup)-op(-i,tup)) *10^(i-1),i=1..nops(tup)) :
%p end:
%p A151963 := proc(n)
%p local tra,x ;
%p tra := [n] ;
%p x := n ;
%p while true do
%p x := A151949(x) ;
%p if x in tra then
%p RETURN(nops(tra)) ;
%p fi;
%p tra := [op(tra),x] :
%p od:
%p end:
%p seq(A151963(n),n=0..120) ;
%t f[n_] := Module[{idn = IntegerDigits@n, idns}, idns = Sort@ idn; FromDigits@ Reverse@ idns - FromDigits@ idns]; g[n_] := Length[ NestWhileList[ f, n, UnsameQ, All]] - 1; Table[g@n, {n, 0, 104}] (* _Robert G. Wilson v_, Aug 20 2009 *)
%Y In other bases: A164886 (base 2), A164996 (base 3), A165015 (base 4), A165035 (base 5), A165054 (base 6), A165074 (base 7), A165093 (base 8), A165113 (base 9). - _Joseph Myers_, Sep 05 2009
%K nonn,base
%O 0,2
%A _N. J. A. Sloane_, Aug 19 2009
%E Typos corrected by _Joseph Myers_, Aug 20 2009
%E More terms from _R. J. Mathar_ and _Robert G. Wilson v_, Aug 20 2009