If you’re feeling a little down today, maybe a happy number will cheer you up. To see if an integer is happy, start by squaring its digits (in base ten, though happiness is defined analogously in other bases as well) and adding them together. So the number 23 would become 13 because 22+32=4+9=13. Now iterate the process. For 13, we get 1+9=10. Then 10 becomes 1, where it stays. If a number eventually gets to 1, it is called happy. If it doesn’t, it will end up in an endless loop, reaching 4, then 16, 37, 58, 89, 145, 42, 20, and 4 again.

Unfortunately, the reason for the moniker “happy” has been lost in the mists of time. But I can certainly see why a number caught in the purgatory of the 4 – 16 – 37 – 58 – 89 – 145 – 42 – 20 – 4 cycle might be unhappy.

It’s pleasant enough to figure out whether numbers are happy just for the sake of it, but I’m writing about happy numbers today because I learned they have a strange property: there is no way to put your finger on just how many happy numbers there are.

At first glance, that sentence is false. There are infinitely many happy numbers. At a bare minimum, all powers of 10 are happy. But if instead of asking for a count of happy numbers we ask for what proportion of whole numbers are happy, the story gets more interesting. Mathematicians use the term asymptotic density for this notion.

Even though I haven’t defined asymptotic density yet, you probably won’t find it hard to believe that the even numbers have asymptotic density 1/2: as we look further and further out on the number line, we will always find that about half of the positive integers we’ve seen are even. It might be off by a little bit — only 40 percent of the whole numbers from 1 through 5 are even, after all — but the more numbers we look at, the less our estimates will deviate from 50 percent.

On the other hand, the asymptotic density of powers of 10 in the integers is 0 because they consistently get further and further apart. There are two powers of ten between 1 and 10, making 20 percent of those numbers, 3 between 1 and 100 (3 percent), only 4 between 1 and 1000 (0.4 percent), and so on. Though powers of 10 will keep showing up forever, they get arbitrarily sparse.

Rigorously, a set of whole numbers has asymptotic density p if, as n becomes arbitrarily large, the proportion of the whole numbers from 1 to n that are in the set approaches p.

Many sets of numbers have a defined asymptotic density, but not all of them do. Sets that do not have a defined density have a tendency toward feast or famine. For example, consider the integers with a first digit of 1. If we look at the one-digit numbers, exactly 1/9 of them start with 1. Then we hit a big run of numbers that start with 1. If we look at their proportion between 1 and 19, we jump up to 11/19. Then looking at the numbers from 1 to 99, we fall all the way down to 11/99, or 1/9 again.

If we try to find an asymptotic density for this set, we discover that if we compute the density just after a long run of numbers that start with 1 — the 100’s, the 1000’s, and so on — the proportion of numbers starting with 1 will be close to 5/9. But if we cut off the count right before hitting one of the big milestones like 100 or 1000, their proportion will be 1/9. From the point of view of asymptotic density, there’s no way to bridge the distance between 1/9 and 5/9. The set of whole numbers beginning with 1 has no density. (Amusingly, in this case having no density is different from having 0 density. It would probably be less ambiguous to say that the set’s asymptotic density is not defined, but where's the fun in that?)

Incidentally, 1/9 is called the lower density of that set of numbers and 5/9 the upper density. Roughly speaking, the lower density is the limit of how sparse you can make the set as you go out on the number line, and upper density is how dense you can make it. A set has a defined asymptotic density if the upper and lower densities match. (Other notions of density yield defined values for the density of sets that fail to have asymptotic density, but we're not worried about them today.)

There are three happy numbers between 1 and 10, 20 between 1 and 100, 143 between 1 and 1,000, and 1442 between 1 and 10,000. (The Online Encyclopedia of Integer Sequences dedicates a sequence to counting the number of happy numbers between 1 and 10n.) It sort of looks like maybe the happy numbers have density around 14 percent. But somewhat surprisingly to me, the asymptotic density of happiness does not exist. Justin Gilmer wrote a paper showing that the lower density of the happy numbers is below 12 percent and the upper density is above 18 percent. (Delightfully, his argument involves “b-happy functions.”) The chasm between lower and upper densities is not as wide as in the case of numbers starting with 1, but it is wide enough to ruin density.

The fact that happy numbers do not have a defined asymptotic density means there are parts of the number line that have more happiness concentrated in them than others. I'm not sure whether that makes me happy or not.

After I told him about happy numbers and their undefinable density, my spouse Jon Chaika used the idea with a local math circle for middle- and high-school students. You can find a pdf of the worksheet here and a guide for teachers here.