%I
%S 1,7,10,13,19,23,28,31,32,44,49,68,70,79,82,86,91,94,97,100,103,109,
%T 129,130,133,139,167,176,188,190,192,193,203,208,219,226,230,236,239,
%U 262,263,280,291,293,301,302,310,313,319,320,326,329,331,338
%N Happy numbers: numbers whose trajectory under iteration of sum of squares of digits map (see A003132) includes 1.
%C Sometimes called friendly numbers, but this usage is deprecated.
%C Gilmer shows that the lower density of this sequence is < 0.1138 and the upper density is > 0.18577.  _Charles R Greathouse IV_, Dec 21 2011
%C Corrected the upper and lower density inequalities in the comment above.  _Nathan Fox_, Mar 14 2013
%C Grundman defines the heights of the happy numbers by the number of iterations needed to reach the 1: 0, 5, 1, 2, 4, 3, 3, 2, 3, 4, 4, 2, 5, 3, 3, 2, 4, 4, 3, 1, ... E.g., for n=2 the height of 7 is 5 because it needs 5 iterations: 7 > 49 > 97 > 130 > 10 > 1.  _R. J. Mathar_, Jul 09 2017
%D L. E. Dickson, History of the Theory of Numbers, Vol, I: Divisibility and Primality, AMS Chelsea Publ., 1999.
%D R. K. Guy, Unsolved Problems Number Theory, Sect. E34.
%D J. N. Kapur, Reflections of a Mathematician, Chap. 34 pp. 319324, Arya Book Depot New Delhi 1996.
%H Jud McCranie, <a href="/A007770/b007770.txt">Table of n, a(n) for n = 1..143071</a>
%H Breeanne Baker Swart, Susan Crook, Helen G. Grundman, Laura HallSeelig, May Mei, and Laurie Zack, <a href="https://arxiv.org/abs/1908.02194">Fixed Points of Augmented Generalized Happy Functions II: Oases and Mirages</a>, arXiv:1908.02194 [math.NT], 2019.
%H T. Cai and X. Zhou, <a href="https://doi.org/10.1216/RMJ20083861921">On the height of happy numbers</a>, Rocky Mt. J. Math. 38 (6) (2008) 1921.
%H E. ElSedy and S. Siksek, <a href="https://doi.org/10.1216/rmjm/1022009281">On happy numbers</a>, Rocky Mountain J. Math. 30 (2000), 565570.
%H Justin Gilmer, <a href="http://arxiv.org/abs/1110.3836">On the density of happy numbers</a>, arXiv:1110.3836 [math.NT], 20112015.
%H H. G. Grundmann, <a href="https://cs.uwaterloo.ca/journals/JIS/VOL13/Grundman/grundman7.html">Semihappy Numbers</a>, J. Int. Seq. 13 (2010), 10.4.8.
%H Hao Pan, <a href="https://arxiv.org/abs/math/0607213">Consecutive happy numbers</a>, arXiv:math/0607213 [math.NT], 2006.
%H W. Schneider, <a href="http://web.archive.org/web/2004/www.wschnei.de/digitrelatednumbers/happynumbers.html">Happy Numbers</a> (Includes list of terms below 10000)
%H R. Styer, <a href="https://cs.uwaterloo.ca/journals/JIS/VOL13/Styer/styer5.html">Smallest Examples of Strings of Consecutive Happy Numbers</a>, J. Int. Seq. 13 (2010), 10.6.3.
%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/HappyNumber.html">Happy Number</a>
%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/Digitaddition.html">Digitaddition</a>
%H Wikipedia, <a href="http://en.wikipedia.org/wiki/Happy_number">Happy number</a>
%F From Ulrich Krug (leuchtfeuer37(AT)gmx.de), Apr 23 2009: (Start)
%F 1) Every power 10^k is a member of the sequence.
%F 2) If n is member the numbers obtained by placing zeros anywhere in n are members.
%F 3) If n is member each permutation of digits of n gives another member.
%F 4) If the repeated process of summing squared digits give a number which is already a member of sequence the starting number belongs to the sequence.
%F 5) If n is a member the repunit consisting of n 1's is a member.
%F 6) If n is a member delete any digit d, new number consisting of remaining digits of n and d^2 1's placed everywhere to n is a member.
%F 7) It is conjectured that the sequence includes an infinite number of primes (see A035497).
%F 8) For any starting number the repeated process of summing squared digits ends with 1 or gives an "8loop" which ends with (37,58,89,145,42,20,4,16,37) (End)
%e 1 is OK. 2 > 4 > 16 > 37 > ... > 4, which repeats with period 8, so never reaches 1, so 2 (and 4) are unhappy.
%e A correspondent suggested that 98 is happy, but it is not. It enters a cycle 98 > 145 > 42 > 20 > 4 > 16 >37 >58 > 89 > 145 ...
%t f[n_] := Total[IntegerDigits[n]^2]; Select[Range[400], NestWhile[f, #, UnsameQ, All] == 1 &] (* _T. D. Noe_, Aug 22 2011 *)
%t Select[Range[1000],FixedPoint[Total[IntegerDigits[#]^2]&,#,10]==1&] (* _Harvey P. Dale_, Oct 09 2011 *)
%t (* A example with recurrence formula to test if a number is happy *)
%t a[1]=7;
%t a[n_]:=Sum[(Floor[a[n1]/10^k]10*Floor[a[n1]/10^(k+1)]) ^ (2) ,{k, 0,
%t Floor[Log[10,a[n1]]] }]
%t Table[a[n],{n,1,10}] (* _José de Jesús Camacho Medina_, Mar 29 2014 *)
%o (Haskell)
%o a007770 n = a007770_list !! (n1)
%o a007770_list = filter ((== 1) . a103369) [1..]
%o  _Reinhard Zumkeller_, Aug 24 2011
%o (PARI) ssd(n)=n=digits(n);sum(i=1,#n,n[i]^2)
%o is(n)=while(n>6,n=ssd(n));n==1 \\ _Charles R Greathouse IV_, Nov 20 2012
%o (Python)
%o def ssd(n): return sum(int(d)**2 for d in str(n))
%o def ok(n):
%o while n not in [1, 4]: n = ssd(n) # iterate until fixed point or in cycle
%o return n==1
%o def aupto(n): return [k for k in range(1, n+1) if ok(k)]
%o print(aupto(338)) # _Michael S. Branicky_, Jan 07 2021
%Y Cf. A003132 (the underlying map), A001273, A035497 (happy primes), A046519, A031177, A002025, A050972, A050973, A074902, A003132, A103369, A035502, A068571, A072494, A124095, A219667, A239320 (base 3), A240849 (base 5).
%K nonn,base,nice,easy
%O 1,2
%A _N. J. A. Sloane_, A.R.McKenzie(AT)bnr.co.uk
