# 快乐数

快乐数是指这样一个正整数：将它替换为其十进制各位数字的平方和，反复进行这一操作，最终会到达 1；而那些迭代后陷入一个不包含 1 的循环的数则是不快乐数（有时也称为悲伤数）。<sup>[1](https://mathworld.wolfram.com/HappyNumber.html)</sup> 例如，7 是快乐数，因为 7 → 49 → 97 → 130 → 10 → 1；而 4 是不快乐数，因为它的序列在八步之后又回到了 4。<sup>[1](https://mathworld.wolfram.com/HappyNumber.html)</sup><sup> • </sup><sup>[2](https://www.scientificamerican.com/article/all-natural-numbers-are-either-happy-or-sad-some-are-narcissistic-too/)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 若对一个正整数反复应用将其映射为各位数字平方和的映射，某次迭代的结果等于 1，则该正整数为快乐数<sup>[1](https://mathworld.wolfram.com/HappyNumber.html)</sup> |
| 可能的终点 | 每次迭代都会到达 0、1、4、16、20、37、42、58、89 或 145 之一（OEIS A039943；波尔热斯 1945）<sup>[1](https://mathworld.wolfram.com/HappyNumber.html)</sup> |
| 不快乐循环 | 所有不快乐数都进入八元循环 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4<sup>[3](https://mathomaly.com/explore/happy/)</sup> |
| 前几项 | 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, ...（OEIS A007770）<sup>[4](https://oeis.org/A007770)</sup> |
| 密度 | 贾斯汀·吉尔默证明其下密度低于 0.1138，上密度高于 0.18577，因此渐近密度不存在<sup>[5](https://arxiv.org/abs/1110.3836)</sup> |
| 连续段 | 埃尔-塞迪与西克塞克证明该序列包含任意长的连续快乐数段<sup>[4](https://oeis.org/A007770)</sup> |
| 编程应用 | LeetCode 第 202 题要求实现快乐数判定；一种标准做法是用 Floyd 循环检测，空间复杂度为 O(1)<sup>[6](https://technohubnetwork.com/dsa/topics/math-geometry/problems/202-happy-number)</sup> |

## 迭代及其终点

将该映射写作 \( S_{2,10}(n) = \sum_{i} a_i^{2} \)，其中 \( a_i \) 是 \( n \) 的十进制各位数字。迭代这个映射不会无限游荡：一个 \( d \) 位数至少是 \( 10^{d-1} \)，而其各位数字的平方和至多为 \( d \cdot 81 \)。前者随 \( d \) 指数增长，后者仅线性增长，因此超过某一宽度后，每个数都严格向下映射，所有轨道都被困在一个有限区域内。<sup>[3](https://mathomaly.com/explore/happy/)</sup> 美国作家阿瑟·波尔热斯当时在一所军校任讲师，他在 1945 年发表于《美国数学月刊》的论文中证明，迭代该映射总会到达 0、1、4、16、20、37、42、58、89 或 145 这十个数之一。<sup>[1](https://mathworld.wolfram.com/HappyNumber.html)</sup><sup> • </sup><sup>[7](https://tangente-mag.com/en/articles/it-doesn-t-take-much-to-be-happy)</sup>

在十进制中使用平方时，只出现两个循环：单个数 1，以及一个长度为八的环，依次为 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 回到 4。<sup>[3](https://mathomaly.com/explore/happy/)</sup> 轨道到达 1 的数是快乐数；其他所有正整数最终都进入这个八元循环，因而都是不快乐数。由于快乐性沿轨道继承，一旦一个数的状态已知，其序列中的每个数都具有相同的状态。<sup>[8](https://archive.lib.msu.edu/crcmath/math/math/h/h065.htm)</sup>

到达 1 所需的迭代次数称为高度。7 的高度是 5，因为 7 → 49 → 97 → 130 → 10 → 1。<sup>[4](https://oeis.org/A007770)</sup> 高度可以很大：78,999 是高度为 7 的最小快乐数，这一数值由理查德·盖伊作为问题提出，海伦·格伦德曼与伊丽莎白·蒂普尔给出了答案，并一并给出了高度不超过 10 的最小快乐数。<sup>[9](https://arxiv.org/abs/1511.01441)</sup>

## 历史

这一概念最早由谁提出尚不清楚。快乐数在 20 世纪 60 年代由英国作家、利兹大学纯数学高级讲师雷金纳德·艾伦比推广开来，他是从女儿那里得知这一概念的，而他的女儿是在学校里遇到的。<sup>[2](https://www.scientificamerican.com/article/all-natural-numbers-are-either-happy-or-sad-some-are-narcissistic-too/)</sup> 最早发表的研究一般认为是波尔热斯 1945 年的文章《一组八个数》，标题指的是不快乐循环中的八个成员。<sup>[7](https://tangente-mag.com/en/articles/it-doesn-t-take-much-to-be-happy)</sup><sup> • </sup><sup>[10](https://fermatslibrary.com/s/a-set-of-eight-numbers)</sup>

## 密度

快乐数的供给是无限的：每个 10 的幂都是快乐数，因为 \( 10^k \) 一步就映射到 1。<sup>[4](https://oeis.org/A007770)</sup> 然而它们的频率并不稳定下来。在前 10 个正整数中有 3 个快乐数，在前 100 个中有 20 个，在前 1,000 个中有 143 个，在前 10,000 个中有 1,442 个，暗示比例接近 14%。<sup>[2](https://www.scientificamerican.com/article/all-natural-numbers-are-either-happy-or-sad-some-are-narcissistic-too/)</sup><sup> • </sup><sup>[11](https://www.scientificamerican.com/blog/roots-of-unity/happy-numbers-have-no-density/)</sup>

贾斯汀·吉尔默当时是一位研究数论中概率方法的数学家，他于 2011 年证明（2013 年发表于《Integers》），这一比例永不收敛：快乐数的下密度低于 0.1138，上密度高于 0.18577，因此渐近密度不存在。<sup>[5](https://arxiv.org/abs/1110.3836)</sup><sup> • </sup><sup>[12](https://www.emis.de/journals/INTEGERS/papers/n48/n48.pdf)</sup> 他的论证使用概率估计构造出快乐数浓度异常高和异常低的长区间，因此数轴的不同部分蕴含着不同数量的快乐。<sup>[11](https://www.scientificamerican.com/blog/roots-of-unity/happy-numbers-have-no-density/)</sup> 吉尔默还证明了对于快乐数的若干推广，渐近密度同样不存在。<sup>[5](https://arxiv.org/abs/1110.3836)</sup>

## 连续段与结构

该序列并非在每一段都稀疏。埃尔-塞迪与西克塞克于 2000 年证明，它包含任意长的连续快乐数子序列，这意味着该序列的上一致密度为 1。<sup>[4](https://oeis.org/A007770)</sup> 潘浩于 2006 年在更一般的 (e, b) 设定下给出了任意长度连续快乐数存在的证明，表明只要对 b − 1 的任一素因子 p，e − 1 不被 p − 1 整除，这样的连续段就存在。<sup>[13](https://arxiv.org/abs/math/0607213)</sup> 相邻快乐数之间的间隔也无界增长；记录间隔从 1、32、49、109、139、409 开始，并有更大的值（OEIS A383291）。<sup>[14](https://oeis.org/A383291)</sup>

## 推广

定义可沿若干方向扩展。将平方换成 e 次幂、将十进制换成 b ≥ 2 进制，就得到格伦德曼与蒂普尔研究的 (e, b)-快乐数；这些快乐函数的循环与不动点已被编目，而 \( S_{2,b} \) 的不动点数目可以用除数函数精确表示。<sup>[15](https://www.researchgate.net/publication/316134753_Cycles_and_fixed_points_of_happy_functions)</sup><sup> • </sup><sup>[16](https://www.fq.math.ca/Scanned/39-5/grundman.pdf)</sup> 这一概念已被推广到负进制和分数进制 \( p/q \)，此时每个数的高度都是有限的，且当 q = 2 或 e = 1 时高度可以任意大。<sup>[17](https://campus.lakeforest.edu/trevino/HappyArticle.pdf)</sup> 2025 年的一种变体“兴奋数”将各位数字平方和替换为首位数字与各位数字平方和的乘积；b-兴奋函数与 b-快乐函数密切相关，但表现明显不同。<sup>[18](https://link.springer.com/article/10.1007/s44007-025-00185-7)</sup>

快乐性既是数的性质，也是记法的性质。在二进制和四进制中使用平方时，每个正整数都是快乐数；而在十二进制中，整个有界区域内只有七个值是快乐数；改变幂次又会改变图景，因为四进制中使用立方时有九个循环，大多数数都是不快乐数。<sup>[3](https://mathomaly.com/explore/happy/)</sup>

## 计算快乐性

判定一个数是否为快乐数是常见的编程练习，出现在 LeetCode 第 202 题和竞赛题集中。<sup>[6](https://technohubnetwork.com/dsa/topics/math-geometry/problems/202-happy-number)</sup><sup> • </sup><sup>[19](https://www.cs.utexas.edu/~scottm/uil/2011/UTCS_UIL_ProgrammingProblems_2011.pdf)</sup> 常用三种标准方法：用哈希集合存储已访问的值，遇到重复即停止；利用每个不快乐轨道都会碰到 4 这一事实，迭代直到值为 1 或 4；或对后继函数应用 Floyd 龟兔循环检测，空间复杂度为 O(1)。<sup>[6](https://technohubnetwork.com/dsa/topics/math-geometry/problems/202-happy-number)</sup><sup> • </sup><sup>[20](https://parikshan.dmj.one/learn/happy-number)</sup>

## References

1. [Happy Number, Wolfram MathWorld](https://mathworld.wolfram.com/HappyNumber.html)
2. [All Natural Numbers Are Either Happy or Sad. Some Are Narcissistic, Too, Scientific American](https://www.scientificamerican.com/article/all-natural-numbers-are-either-happy-or-sad-some-are-narcissistic-too/)
3. [Happy Numbers, Mathomaly](https://mathomaly.com/explore/happy/)
4. [A007770: Happy numbers, OEIS](https://oeis.org/A007770)
5. [Justin Gilmer, On the Density of Happy Numbers, arXiv:1110.3836](https://arxiv.org/abs/1110.3836)
6. [202. Happy Number, THN Interview Prep](https://technohubnetwork.com/dsa/topics/math-geometry/problems/202-happy-number)
7. [It doesn't take much to be happy, Tangente Magazine](https://tangente-mag.com/en/articles/it-doesn-t-take-much-to-be-happy)
8. [Happy Number, CRC archive](https://archive.lib.msu.edu/crcmath/math/math/h/h065.htm)
9. [Numbers and the Heights of their Happiness, arXiv:1511.01441](https://arxiv.org/abs/1511.01441)
10. [A Set of Eight Numbers, annotated at Fermat's Library](https://fermatslibrary.com/s/a-set-of-eight-numbers)
11. [Happy Numbers Have No Density, Scientific American (Evelyn Lamb)](https://www.scientificamerican.com/blog/roots-of-unity/happy-numbers-have-no-density/)
12. [On the Density of Happy Numbers, Integers 13 (2013), A48](https://www.emis.de/journals/INTEGERS/papers/n48/n48.pdf)
13. [Hao Pan, On consecutive happy numbers, arXiv:math/0607213](https://arxiv.org/abs/math/0607213)
14. [A383291: Successively larger gaps in happy numbers, OEIS](https://oeis.org/A383291)
15. [Cycles and fixed points of happy functions, ResearchGate](https://www.researchgate.net/publication/316134753_Cycles_and_fixed_points_of_happy_functions)
16. [H. G. Grundman and E. A. Teeple, Generalized Happy Numbers, Fibonacci Quarterly 39 (2001)](https://www.fq.math.ca/Scanned/39-5/grundman.pdf)
17. [Enrique Treviño and Mikita Zhylinski, On generalizing happy numbers to fractional base number systems](https://campus.lakeforest.edu/trevino/HappyArticle.pdf)
18. [Elated Numbers, La Matematica (Springer, 2025)](https://link.springer.com/article/10.1007/s44007-025-00185-7)
19. [UTCS UIL Programming Problems 2011](https://www.cs.utexas.edu/~scottm/uil/2011/UTCS_UIL_ProgrammingProblems_2011.pdf)
20. [Happy number, Parikshan](https://parikshan.dmj.one/learn/happy-number)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Integer sequences and partitions › Special and named integers › Digit-based named numbers

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/happy-number

English original: https://www.edgepedia.org/happy-number
