%I #78 Mar 07 2023 07:58:09
%S 0,0,0,0,0,0,0,0,0,0,9,0,9,18,27,36,45,54,63,72,18,9,0,9,18,27,36,45,
%T 54,63,27,18,9,0,9,18,27,36,45,54,36,27,18,9,0,9,18,27,36,45,45,36,27,
%U 18,9,0,9,18,27,36,54,45,36,27,18,9,0,9,18,27,63,54,45,36,27,18,9,0,9,18,72,63,54,45,36,27,18,9,0,9,81,72,63,54,45,36,27,18,9,0,99,99,198,297,396,495,594,693,792,891,99,0,99,198,297,396,495,594,693,792
%N a(n) = image of n under the Kaprekar map n -> (n with digits sorted into descending order) - (n with digits sorted into ascending order).
%C Entries are multiples of 9 - see A151950.
%C a(n) = A004186(n) - A004185(n); a(A010785(n)) = 0. - _Reinhard Zumkeller_, corrected: Mar 23 2015, Jul 09 2013
%H Indranil Ghosh, <a href="/A151949/b151949.txt">Table of n, a(n) for n = 0..20000</a> (terms 0..1000 from Joseph Myers and Robert G. Wilson v)
%H H. Hanslik, E. Hetmaniok, I. Sobstyl, et al., <a href="http://yadda.icm.edu.pl/baztech/element/bwmeta1.element.baztech-aeb2e2a6-99ca-4268-8f6b-a947b9c04da9">Orbits of the Kaprekar's transformations-some introductory facts</a>, Zeszyty Naukowe Politechniki Śląskiej, Seria: Matematyka Stosowana z. 5, Nr kol. 1945; 2015.
%H M. Kauers and C. Koutschan, <a href="https://arxiv.org/abs/2303.02793">Some D-finite and some possibly D-finite sequences in the OEIS</a>, arXiv:2303.02793 [cs.SC], 2023.
%H R. J. Mathar, <a href="/A151959/a151959.txt">Maple code for A151949 and A151959</a>
%H Joseph Myers, <a href="/A151949/a151949.c.txt">C program for computing sequences related to the Kaprekar map</a>
%H Joseph Myers, <a href="/A099009/a099009.txt">List of cycles under Kaprekar map</a> (all numbers with <= 60 digits; cycles are represented by their smallest value)
%H <a href="/index/K#Kaprekar_map">Index entries for the Kaprekar map</a>
%e For n = 15, a(15) = 51 - 15 = 36. - _Indranil Ghosh_, Feb 01 2017
%t f[n_] := Module[{idn = IntegerDigits@n, idns}, idns = Sort@ idn; FromDigits@ Reverse@ idns - FromDigits@ idns]; Table[ f@n, {n, 0, 200}] (* _Harvey P. Dale_, Aug 18 2009 *)
%t Flatten[Table[Differences[FromDigits /@ {y = Sort[x = IntegerDigits[n]], Reverse[y]}], {n, 0, 74}]] (* _Jayanta Basu_, Jul 11 2013 *)
%o (Haskell)
%o a151949 n = a004186 n - a004185 n
%o -- _Reinhard Zumkeller_, corrected: Mar 23 2015, Jul 09 2013
%o (Python)
%o def A151949(n):
%o return int("".join(sorted(str(n),reverse=True)))-int("".join(sorted(str(n)))) # _Indranil Ghosh_, Feb 01 2017
%o (PARI) a(n) = {my(d=digits(n)); fromdigits(vecsort(d,,4)) - fromdigits(vecsort(d));} \\ _Michel Marcus_, Dec 08 2019
%Y In other bases: A164884 (base 2), A164993 (base 3), A165012 (base 4), A165032 (base 5), A165051 (base 6), A165071 (base 7), A165090 (base 8), A165110 (base 9). - _Joseph Myers_, Sep 05 2009
%Y Cf. A151959, A069746, A347688.
%Y Cf. also A004185, A004186, A099009 (fixed points).
%K nonn,base,look
%O 0,11
%A _N. J. A. Sloane_, Aug 18 2009
%E More terms from _Robert G. Wilson v_, Aug 19 2009
%E More than the usual number of terms are shown in order to distinguish this from similar sequences. - _N. J. A. Sloane_, Sep 22 2021