Happy Number

Author

Eric W. Weisstein
March 24, 2006

This notebook downloaded from http://mathworld.wolfram.com/notebooks/IntegerSequences/HappyNumber.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/HappyNumber.html.

©2006 Wolfram Research, Inc. except for portions noted otherwise

Code

HappyList[n_]:=NestWhileList[Total[IntegerDigits[#]^2]&,n,UnsameQ,All]
HappyNumber[n_]:=NestWhile[Total[IntegerDigits[#]^2]&,n,UnsameQ,All]
HappyQ[n_]:=HappyNumber[n]==1

Eventual Numbers Reached

Union[HappyNumber/@Range[0,100]]

Union[HappyNumber/@Range[0,10^5]]

Happy Numbers

Take[happy=Select[Range[10^6],HappyQ],30]//Timing

HappyList/@Take[happy,5]//ColumnForm

Length/@HappyList/@Take[happy,20]-2

ListPlot[Length/@HappyList/@Take[happy,All]-2,PlotStyle->Red]

ListPlot[MapIndexed[{#1,#2[[1]]}&,happy],PlotStyle->Red]

Table[Count[happy,_?(#≤10^n&)],{n,6}]

ListPlot[Log[{1, 3, 20, 143, 1442, 14377, 143071, 1418854, 14255667, 145674808, 1492609148, 15091199357, 149121303586, 1443278000870, 13770853279685, 130660965862333, 1245219117260664, 12024696404768025, 118226055080025491, 1183229962059381238, 12005034444292997294}]]

Consecutive Happy Numbers

Take[Select[Partition[happy,2,1],Subtract@@#==-1&],20]

First/@%

Sets of three consecutive happy numbers

Select[Partition[happy,3,1],#[[-1]]-#[[1]]==2&]

First/@%

Happy Primes

Select[Prime/@Range[1000],HappyQ]//Timing