%I #9 Dec 05 2014 04:52:52
%S 0,495,6174,62964,420876,7509843,64308654,753098643,6431088654,
%T 86420987532,643330866654,8764209875322,64333308666654,
%U 885432098765412,6543331088666544,88543320987665412,975533110888664421
%N Iterate the Kaprekar map of A151949 starting at the n-digit number 100...02; sequence gives the lowest number in the resulting cycle.
%H Joseph Myers, <a href="/A151957/b151957.txt">Table of n, a(n) for n=2..1000</a> [From _Joseph Myers_, Aug 21 2009]
%H <a href="/index/K#Kaprekar_map">Index entries for the Kaprekar map</a>
%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 A151957 := proc(n)
%p local tra,x ;
%p x := 10^(n-1)+2 ;
%p tra := [x] ;
%p while true do
%p x := A151949(x) ;
%p if member(x,tra,'l') then
%p op(l..nops(tra),tra) ;
%p RETURN(min(%)) ;
%p fi;
%p tra := [op(tra),x] :
%p od:
%p end:
%p seq(A151957(n),n=2..60) ;
%p # _R. J. Mathar_, Aug 20 2009
%t To find the first 20 terms of the trajectory of 10002, for instance:
%t f[n_]:=Module[{idn=IntegerDigits[n],idns},idns=Sort[idn];Abs[FromDigits[ idns]-FromDigits[Reverse[idns]]]]
%t NestList[f,10002,20]
%Y See A151958 for the length of the cycles. Cf. A151949, A151955 (the trajectory of 102), A151956 (the trajectory of 1002).
%Y See also A151967, A151968.
%K nonn,base
%O 2,2
%A _Harvey P. Dale_ and _N. J. A. Sloane_, Aug 18 2009, Aug 19 2009
%E Extended by _R. J. Mathar_ and _Joseph Myers_, Aug 20 2009