Счастливые числа
Numbers that reach 1 through iterated digit-square sums: where math meets joy
A happy number is defined by a simple process: take any positive integer, replace it with the sum of the squares of its digits, and repeat. If this process eventually reaches 1, the number is happy. If it loops endlessly without ever reaching 1, the number is unhappy. This deceptively simple concept leads to rich mathematical structures and connections to prime numbers.
Как работает алгоритм счастья?
The process is straightforward: take each digit of the number, square it, and add all the squares together. Then repeat with the result. Every number either reaches 1 (happy) or enters the cycle 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 (unhappy).
Example: Is 23 happy?
¡23 is a happy number!
Example: Is 4 happy?
¡4 is an unhappy number -- it enters an infinite loop!
Свойства счастливых чисел
- The first few happy numbers are: 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100...
- Approximately 14.3% of all positive integers are happy numbers.
- Every unhappy number eventually enters the same 8-number cycle: 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20.
- If a number is happy, then every number in its iteration sequence is also happy.
Счастливые простые числа
A happy prime is a number that is both happy and prime. These doubly special numbers combine two independent mathematical properties. They were featured in a 2007 episode of Doctor Who, bringing recreational mathematics to mainstream television.
Счастливые числа в культуре
Happy numbers gained popular attention when they appeared in the Doctor Who episode "42" (2007), where the crew of a spaceship had to identify happy primes to unlock security doors. The concept was introduced mathematically by Reg Allenby in 1966 and has since become a popular topic in recreational mathematics and programming challenges.
Happy Numbers up to 500
There are 76 happy numbers between 1 and 500. Click on any to see its full analysis.
Вы знали
- Exactly one cycle (of length 8) contains all sad numbers: [4,16,37,58,89,145,42,20]. Every unhappy number eventually reaches this cycle regardless of starting value. This unique cycle structure means the happy/sad binary classification is absolute—no number occupies middle ground; every number is either happy or eventually reaches this specific cycle.
- The proportion of happy numbers among first n integers stabilizes around 14.3% for all tested ranges. Among 1 to 143, exactly 20 are happy (13.9%); among 1 to 1000, approximately 143 are happy (14.3%). This remarkable stability across different scales suggests fundamental mathematical structure underlying happy number distribution.
- The smallest happy number is 1, the next smallest is 7. But 10, appearing before 13, surprises many—10→1²+0²=1 makes it happy. This demonstrates how leading zeros in digit squaring affect outcomes; positioning and digit values matter significantly.
- In base 2 (binary), the concept generalizes: happy numbers can be defined for any base, though the definition requires modification for bases where cyclic behavior differs. Some bases have no cycles, some single cycles, others multiple cycles. The structural difference between bases reveals how abstract the happy number concept truly is.
- The mathematical literature on happy numbers is surprisingly limited despite simple definitions, suggesting the concept remains underexplored compared to classical number types. Papers addressing happy number properties remain scarce, with major questions about distribution and theoretical foundations remaining open.
Preguntas Frecuentes
How do you determine if a number is happy?
Apply the digit-squaring algorithm: take any positive integer, replace it with the sum of squares of its digits, and repeat. Track results in a set. If reaching 1, the number is happy. If encountering a previously seen value (detecting a cycle), the number is unhappy. For example, 23: 2²+3²=13; 1²+3²=10; 1²+0²=1 (happy). For 2: 2²=4; 4²=16; 1²+6²=37; 3²+7²=58; 5²+8²=89; 8²+9²=145; 1²+4²+5²=42; 4²+2²=20; 2²+0²=4 (cycle detected at 4, unhappy). Most implementations use a set to track seen values, enabling cycle detection upon re-encountering. Programming requires iteration handling, digit extraction, and summation. Manual calculation becomes tedious for large numbers with many iterations.
Why are there exactly 20 happy numbers from 1 to 143?
This represents empirical observation of happy number distribution rather than following obvious pattern. The specific count (20/143 ≈ 14%) matches the long-term frequency (approximately 1 in 7 numbers). The relatively uniform distribution across different ranges suggests happy numbers distribute independently of digit patterns. Different ranges contain varying happy number patterns; some ranges might have 13 in 130 numbers, others 21 in 150 numbers—but averaging across large ranges yields consistent ~14.3% proportion. No closed formula predicts how many happy numbers exist up to n; the count requires iterative checking. The stability of this proportion across ranges, despite lacking simple explanation, demonstrates deep mathematical structure. Research continues investigating why this proportion stabilizes so predictably.
What exactly is the cycle that unhappy numbers reach?
The unhappy cycle in base 10 is [4, 16, 37, 58, 89, 145, 42, 20], of length 8. Starting from any unhappy number, repeated digit-squaring and summation eventually produces one of these values, after which the sequence cycles through these eight numbers repeatedly. For example, 2→4→16→37→58→89→145→42→20→4→16→... Every unhappy number is guaranteed to reach this cycle regardless of starting value. No other cycles exist in base 10 (proven through exhaustive analysis). The uniqueness of this cycle means all unhappy numbers share identical ultimate behavior—cyclical repetition through the same eight-element sequence. This structure contrasts with happy numbers reaching 1 directly. The deterministic behavior of sad numbers makes them mathematically interesting—despite varied paths, all converge to identical behavior.