<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Happy_number</id>
	<title>Happy number - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Happy_number"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Happy_number&amp;action=history"/>
	<updated>2026-06-06T10:58:33Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Happy_number&amp;diff=332457&amp;oldid=prev</id>
		<title>imported&gt;Mikeblas: /* Happy primes */ anchor for link from List of prime numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Happy_number&amp;diff=332457&amp;oldid=prev"/>
		<updated>2025-10-17T16:01:07Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Happy primes: &lt;/span&gt; anchor for link from &lt;a href=&quot;/index.php/List_of_prime_numbers&quot; title=&quot;List of prime numbers&quot;&gt;List of prime numbers&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{short description|Numbers with a certain property involving recursive summation}}&lt;br /&gt;
{{distinguish|text=[[Harshad number]] (derived from Sanskrit &amp;#039;&amp;#039;harśa&amp;#039;&amp;#039; meaning &amp;quot;great joy&amp;quot;)}}&lt;br /&gt;
{{Use dmy dates|date=October 2020}}&lt;br /&gt;
[[File:DessinArbreHeureux01.png|thumb|Tree showing all happy numbers up to 100, with 130 seen with 13 and 31]]&lt;br /&gt;
In [[number theory]], a &amp;#039;&amp;#039;&amp;#039;happy number&amp;#039;&amp;#039;&amp;#039; is a number which eventually reaches 1 when the number is replaced by the sum of the square of each digit. For instance, 13 is a happy number because &amp;lt;math&amp;gt;1^2+3^2=10&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;1^2+0^2=1&amp;lt;/math&amp;gt;. On the other hand, 4 is not a happy number because the sequence starting with &amp;lt;math&amp;gt;4^2=16&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;1^2+6^2=37&amp;lt;/math&amp;gt; eventually reaches &amp;lt;math&amp;gt;2^2+0^2=4&amp;lt;/math&amp;gt;, the number that started the sequence, and so the process continues in an infinite cycle without ever reaching 1. A number which is not happy is called &amp;#039;&amp;#039;&amp;#039;sad&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;unhappy&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
More generally, a &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-&amp;#039;&amp;#039;&amp;#039;happy number&amp;#039;&amp;#039;&amp;#039; is a [[natural number]] in a given [[number base]] &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt; that eventually reaches 1 when iterated over the [[Perfect digital invariant|perfect digital invariant function]] for &amp;lt;math&amp;gt;p = 2&amp;lt;/math&amp;gt;.&amp;lt;ref&amp;gt;{{cite web|url=http://mathworld.wolfram.com/SadNumber.html|title=Sad Number|publisher=Wolfram Research, Inc.|access-date=2009-09-16}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The origin of happy numbers is not clear.  Happy numbers were brought to the attention of [[Reg Allenby]] (a British author and senior lecturer in [[pure mathematics]] at [[Leeds University]]) by his daughter, who had learned of them at school.  However, they &amp;quot;may have originated in Russia&amp;quot; {{harvcol|Guy|2004|p=§E34}}.&lt;br /&gt;
&lt;br /&gt;
== Happy numbers and perfect digital invariants ==&lt;br /&gt;
{{See also|Perfect digital invariant}}&lt;br /&gt;
Formally, let &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; be a natural number. Given the [[Perfect digital invariant|perfect digital invariant function]] &lt;br /&gt;
:&amp;lt;math&amp;gt;F_{p, b}(n) = \sum_{i=0}^{\lfloor \log_{b}{n} \rfloor} {\left(\frac{n \bmod{b^{i+1}} - n \bmod{b^{i}}}{b^{i}}\right)}^p&amp;lt;/math&amp;gt;.&lt;br /&gt;
for base &amp;lt;math&amp;gt;b &amp;gt; 1&amp;lt;/math&amp;gt;, a number &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-happy if there exists a &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;F_{2, b}^j(n) = 1&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;F_{2, b}^j&amp;lt;/math&amp;gt; represents the &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;-th [[Iterated function|iteration]] of &amp;lt;math&amp;gt;F_{2, b}&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-unhappy otherwise. If a number is a [[#Perfect digital invariant|nontrivial perfect digital invariant]] of &amp;lt;math&amp;gt;F_{2, b}&amp;lt;/math&amp;gt;, then it is &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-unhappy.&lt;br /&gt;
&lt;br /&gt;
For example, 19 is 10-happy, as&lt;br /&gt;
: &amp;lt;math&amp;gt;F_{2, 10}(19) = 1^2 + 9^2 = 82&amp;lt;/math&amp;gt;&lt;br /&gt;
: &amp;lt;math&amp;gt;F_{2, 10}^2(19) = F_{2, 10}(82) = 8^2 + 2^2 = 68&amp;lt;/math&amp;gt;&lt;br /&gt;
: &amp;lt;math&amp;gt;F_{2, 10}^3(19) = F_{2, 10}(68) = 6^2 + 8^2 = 100&amp;lt;/math&amp;gt;&lt;br /&gt;
: &amp;lt;math&amp;gt;F_{2, 10}^4(19) = F_{2, 10}(100) = 1^2 + 0^2 + 0^2 = 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, 347 is 6-happy, as&lt;br /&gt;
: &amp;lt;math&amp;gt;F_{2, 6}(347) = F_{2, 6}(1335_6) = 1^2 + 3^2 + 3^2 + 5^2 = 44&amp;lt;/math&amp;gt;&lt;br /&gt;
: &amp;lt;math&amp;gt;F_{2, 6}^2(347) = F_{2, 6}(44) = F_{2, 6}(112_6) = 1^2 + 1^2 + 2^2 = 6&amp;lt;/math&amp;gt;&lt;br /&gt;
: &amp;lt;math&amp;gt;F_{2, 6}^3(347) = F_{2, 6}(6) = F_{2, 6}(10_6) = 1^2 + 0^2 = 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are infinitely many &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-happy numbers, as 1 is a &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-happy number, and for every &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b^n&amp;lt;/math&amp;gt; (&amp;lt;math&amp;gt;10^n&amp;lt;/math&amp;gt; in base &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;) is &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-happy, since its sum is 1. The &amp;#039;&amp;#039;happiness&amp;#039;&amp;#039; of a number is preserved by removing or inserting zeroes at will, since they do not contribute to the cross sum.&lt;br /&gt;
&lt;br /&gt;
=== Natural density of &amp;#039;&amp;#039;b&amp;#039;&amp;#039;-happy numbers ===&lt;br /&gt;
By inspection of the first million or so 10-happy numbers, it appears that they have a [[natural density]] of around 0.15. Perhaps surprisingly, then, the 10-happy numbers do not have an asymptotic density. The upper density of the happy numbers is greater than 0.18577, and the lower density is less than 0.1138.&amp;lt;ref&amp;gt;{{Cite journal |title=On the Density of Happy Numbers |journal=Integers |volume=13 |issue=2 |arxiv=1110.3836 |last1=Gilmer |first1=Justin |year=2013 |page=2 |bibcode=2011arXiv1110.3836G}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Happy bases ===&lt;br /&gt;
{{unsolved|mathematics|Are [[base 2]] and [[base 4]] the only bases that are happy?}}&lt;br /&gt;
A happy base is a number base &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt; where every number is &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-happy. The only happy integer bases less than {{val|5e8}} are [[base 2]] and [[base 4]].&amp;lt;ref&amp;gt;{{Cite OEIS|sequencenumber=A161872|name=Smallest unhappy number in base n}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Specific &amp;#039;&amp;#039;b&amp;#039;&amp;#039;-happy numbers==&lt;br /&gt;
===4-happy numbers===&lt;br /&gt;
For &amp;lt;math&amp;gt;b = 4&amp;lt;/math&amp;gt;, the only positive perfect digital invariant for &amp;lt;math&amp;gt;F_{2, b}&amp;lt;/math&amp;gt; is the trivial perfect digital invariant 1, and there are no other cycles. Because all numbers are [[Periodic point#Iterated functions|preperiodic points]] for &amp;lt;math&amp;gt;F_{2, b}&amp;lt;/math&amp;gt;, all numbers lead to 1 and are happy. As a result, [[base 4]] is a happy base.&lt;br /&gt;
&lt;br /&gt;
===6-happy numbers===&lt;br /&gt;
For &amp;lt;math&amp;gt;b = 6&amp;lt;/math&amp;gt;, the only positive perfect digital invariant for &amp;lt;math&amp;gt;F_{2, b}&amp;lt;/math&amp;gt; is the trivial perfect digital invariant 1, and the only cycle is the eight-number cycle&lt;br /&gt;
: 5 → 41 → 25 → 45 → 105 → 42 → 32 → 21 → 5 → ...&lt;br /&gt;
and because all numbers are preperiodic points for &amp;lt;math&amp;gt;F_{2, b}&amp;lt;/math&amp;gt;, all numbers either lead to 1 and are happy, or lead to the cycle and are unhappy. Because base 6 has no other perfect digital invariants except for 1, no positive integer other than 1 is the sum of the squares of its own digits.&lt;br /&gt;
&lt;br /&gt;
In base 10, the 74 6-happy numbers up to 1296 = 6&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt; are (written in base 10):&lt;br /&gt;
: 1, 6, 36, 44, 49, 79, 100, 160, 170, 216, 224, 229, 254, 264, 275, 285, 289, 294, 335, 347, 355, 357, 388, 405, 415, 417, 439, 460, 469, 474, 533, 538, 580, 593, 600, 608, 628, 638, 647, 695, 707, 715, 717, 767, 777, 787, 835, 837, 847, 880, 890, 928, 940, 953, 960, 968, 1010, 1018, 1020, 1033, 1058, 1125, 1135, 1137, 1168, 1178, 1187, 1195, 1197, 1207, 1238, 1277, 1292, 1295&lt;br /&gt;
&lt;br /&gt;
===10-happy numbers===&lt;br /&gt;
For &amp;lt;math&amp;gt;b = 10&amp;lt;/math&amp;gt;, the only positive perfect digital invariant for &amp;lt;math&amp;gt;F_{2, b}&amp;lt;/math&amp;gt; is the trivial perfect digital invariant 1, and the only cycle is the eight-number cycle&lt;br /&gt;
: 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 → ...&lt;br /&gt;
and because all numbers are preperiodic points for &amp;lt;math&amp;gt;F_{2, b}&amp;lt;/math&amp;gt;, all numbers either lead to 1 and are happy, or lead to the cycle and are unhappy. Because base 10 has no other perfect digital invariants except for 1, no positive integer other than 1 is the sum of the squares of its own digits.&lt;br /&gt;
&lt;br /&gt;
In base 10, the 143 10-happy numbers up to 1000 are:&lt;br /&gt;
: 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130, 133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239, 262, 263, 280, 291, 293, 301, 302, 310, 313, 319, 320, 326, 329, 331, 338, 356, 362, 365, 367, 368, 376, 379, 383, 386, 391, 392, 397, 404, 409, 440, 446, 464, 469, 478, 487, 490, 496, 536, 556, 563, 565, 566, 608, 617, 622, 623, 632, 635, 637, 638, 644, 649, 653, 655, 656, 665, 671, 673, 680, 683, 694, 700, 709, 716, 736, 739, 748, 761, 763, 784, 790, 793, 802, 806, 818, 820, 833, 836, 847, 860, 863, 874, 881, 888, 899, 901, 904, 907, 910, 912, 913, 921, 923, 931, 932, 937, 940, 946, 964, 970, 973, 989, 998, 1000 {{OEIS|id=A007770}}.&lt;br /&gt;
&lt;br /&gt;
The distinct combinations of digits that form 10-happy numbers below 1000 are (the rest are just rearrangements and/or insertions of zero digits):&lt;br /&gt;
: 1, 7, 13, 19, 23, 28, 44, 49, 68, 79, 129, 133, 139, 167, 188, 226, 236, 239, 338, 356, 367, 368, 379, 446, 469, 478, 556, 566, 888, 899. {{OEIS|id=A124095}}.&lt;br /&gt;
&lt;br /&gt;
The first pair of consecutive 10-happy numbers is 31 and 32.&amp;lt;ref&amp;gt;{{cite OEIS |1=A035502 |2=Lower of pair of consecutive happy numbers |access-date=8 April 2011}}&amp;lt;/ref&amp;gt; The first set of three consecutive is 1880, 1881, and 1882.&amp;lt;ref&amp;gt;{{cite OEIS |1=A072494 |2=First of triples of consecutive happy numbers |access-date=8 April 2011}}&amp;lt;/ref&amp;gt; It has been proven that there exist sequences of consecutive happy numbers of any natural number length.&amp;lt;ref&amp;gt;{{Cite arXiv |title=Consecutive Happy Numbers |eprint=math/0607213 |last1=Pan |first1=Hao |year=2006 }}&amp;lt;/ref&amp;gt; The beginning of the first run of at least &amp;#039;&amp;#039;n&amp;#039;&amp;#039; consecutive 10-happy numbers for &amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;amp;nbsp;=&amp;amp;thinsp;1, 2, 3, ... is&amp;lt;ref name=&amp;quot;Sloane-A055629&amp;quot;&amp;gt;{{Cite OEIS |1=A055629 |2=Beginning of first run of at least &amp;#039;&amp;#039;n&amp;#039;&amp;#039; consecutive happy numbers}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
: 1, 31, 1880, 7839, 44488, 7899999999999959999999996, 7899999999999959999999996, ...&lt;br /&gt;
As Robert Styer puts it in his paper calculating this series: &amp;quot;Amazingly, the same value of N that begins the least sequence of six consecutive happy numbers also begins the least sequence of seven consecutive happy numbers.&amp;quot;&amp;lt;ref&amp;gt;{{cite journal |last=Styer |first=Robert |year=2010 |page=5 |title=Smallest Examples of Strings of Consecutive Happy Numbers |journal=[[Journal of Integer Sequences]] |volume=13 |id=10.6.3 |url=https://cs.uwaterloo.ca/journals/JIS/VOL13/Styer/styer5.html |via=[[University of Waterloo]]}} Cited in {{harvtxt|Sloane &amp;quot;A055629&amp;quot;}}.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The number of 10-happy numbers up to 10&amp;lt;sup&amp;gt;&amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;lt;/sup&amp;gt; for 1&amp;amp;nbsp;≤&amp;amp;nbsp;&amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;amp;nbsp;≤&amp;amp;nbsp;20 is&amp;lt;ref&amp;gt;{{Cite OEIS |1=A068571 |2=Number of happy numbers &amp;lt;= 10^n}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
: 3, 20, 143, 1442, 14377, 143071, 1418854, 14255667, 145674808, 1492609148, 15091199357, 149121303586, 1443278000870, 13770853279685, 130660965862333, 1245219117260664, 12024696404768025, 118226055080025491, 1183229962059381238, 12005034444292997294.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;span class=&amp;quot;anchor&amp;quot; id=&amp;quot;Happy primes&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;Happy primes==&lt;br /&gt;
A &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-happy prime is a number that is both &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-happy and [[prime number|prime]]. Unlike happy numbers, rearranging the digits of a &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt;-happy prime will not necessarily create another happy prime. For instance, while 19 is a 10-happy prime, 91&amp;amp;nbsp;=&amp;amp;nbsp;13&amp;amp;nbsp;×&amp;amp;nbsp;7 is not prime (but is still 10-happy).&lt;br /&gt;
&lt;br /&gt;
All prime numbers are 2-happy and 4-happy primes, as [[base 2]] and [[base 4]] are happy bases.&lt;br /&gt;
&lt;br /&gt;
===6-happy primes===&lt;br /&gt;
In [[base 6]], the 6-happy primes below 1296 = 6&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt; are&lt;br /&gt;
:211, 1021, 1335, 2011, 2425, 2555, 3351, 4225, 4441, 5255, 5525&lt;br /&gt;
&lt;br /&gt;
===10-happy primes===&lt;br /&gt;
In [[base 10]], the 10-happy primes below 500 are&lt;br /&gt;
:7, 13, 19, 23, 31, 79, 97, 103, 109, 139, 167, 193, 239, 263, 293, 313, 331, 367, 379, 383, 397, 409, 487 {{OEIS|id=A035497}}.&lt;br /&gt;
&lt;br /&gt;
The [[palindromic prime]] {{nowrap|10&amp;lt;sup&amp;gt;150006&amp;lt;/sup&amp;gt; + {{val|7426247e75000}} + 1}} is a 10-happy prime with {{val|150,007}} digits because the many 0s do not contribute to the sum of squared digits, and {{nowrap|1&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + 7&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + 4&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + 2&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + 6&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + 2&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + 4&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + 7&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + 1&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;}} =&amp;amp;nbsp;176, which is a 10-happy number. Paul Jobling discovered the prime in 2005.&amp;lt;ref&amp;gt;{{cite web |url=http://primes.utm.edu/primes/page.php?id=76550 |title=The Prime Database: 10&amp;lt;sup&amp;gt;150006&amp;lt;/sup&amp;gt; + 7426247 · 10&amp;lt;sup&amp;gt;75000&amp;lt;/sup&amp;gt; + 1 |author=Chris K. Caldwell |work=utm.edu}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{As of|2010}}, the largest known 10-happy prime is 2&amp;lt;sup&amp;gt;42643801&amp;lt;/sup&amp;gt;&amp;amp;nbsp;−&amp;amp;nbsp;1 (a [[Mersenne prime]]).{{Dubious|date=December 2017}} Its decimal expansion has {{val|12,837,064}} digits.&amp;lt;ref&amp;gt;{{cite web |url=http://primes.utm.edu/primes/page.php?id=88847 |title=The Prime Database: 2&amp;lt;sup&amp;gt;42643801&amp;lt;/sup&amp;gt; − 1 |author=Chris K. Caldwell |work=utm.edu}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===12-happy primes===&lt;br /&gt;
&lt;br /&gt;
In [[base 12]], there are no 12-happy primes less than 10000, the first 12-happy primes are (the letters X and E represent the decimal numbers 10 and 11 respectively)&lt;br /&gt;
&lt;br /&gt;
:11031, 1233E, 13011, 1332E, 16377, 17367, 17637, 22E8E, 2331E, 233E1, 23955, 25935, 25X8E, 28X5E, 28XE5, 2X8E5, 2E82E, 2E8X5, 31011, 31101, 3123E, 3132E, 31677, 33E21, 35295, 35567, 35765, 35925, 36557, 37167, 37671, 39525, 4878E, 4X7X7, 53567, 55367, 55637, 56357, 57635, 58XX5, 5X82E, 5XX85, 606EE, 63575, 63771, 66E0E, 67317, 67371, 67535, 6E60E, 71367, 71637, 73167, 76137, 7XX47, 82XE5, 82EX5, 8487E, 848E7, 84E87, 8874E, 8X1X7, 8X25E, 8X2E5, 8X5X5, 8XX17, 8XX71, 8E2X5, 8E847, 92355, 93255, 93525, 95235, X1X87, X258E, X285E, X2E85, X85X5, X8X17, XX477, XX585, E228E, E606E, E822E, EX825, ...&lt;br /&gt;
&lt;br /&gt;
==Programming example==&lt;br /&gt;
The examples below implement the perfect digital invariant function for &amp;lt;math&amp;gt;p = 2&amp;lt;/math&amp;gt; and a default base &amp;lt;math&amp;gt;b = 10&amp;lt;/math&amp;gt; described in the definition of happy given at the top of this article, repeatedly; after each time, they check for both halt conditions: reaching 1, and [[Cycle detection|repeating a number]].&lt;br /&gt;
&lt;br /&gt;
A simple test in [[Python (programming language)|Python]] to check if a number is happy:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def pdi_function(number, base: int = 10):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Perfect digital invariant function.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    total = 0&lt;br /&gt;
    while number &amp;gt; 0:&lt;br /&gt;
        total += pow(number % base, 2)&lt;br /&gt;
        number = number // base&lt;br /&gt;
    return total&lt;br /&gt;
&lt;br /&gt;
def is_happy(number: int) -&amp;gt; bool:&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Determine if the specified number is a happy number.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    seen_numbers = set()&lt;br /&gt;
    while number &amp;gt; 1 and number not in seen_numbers:&lt;br /&gt;
        seen_numbers.add(number)&lt;br /&gt;
        number = pdi_function(number)&lt;br /&gt;
    return number == 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== In popular culture ==&lt;br /&gt;
&lt;br /&gt;
* In 2007, the concept of happy numbers was used in &amp;#039;&amp;#039;[[Professor Layton and the Diabolical Box]]&amp;#039;&amp;#039;, in puzzle 149 (&amp;quot;Number Cycle&amp;quot;), using the sequence beginning with 4, which repeats every 8 terms. &lt;br /&gt;
* In the [[Doctor Who]] episode [[42 (Doctor Who)|42]], a sequence of happy primes is the password to open a door. &lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Arithmetic dynamics#Other areas in which number theory and dynamics interact|Arithmetic dynamics]]&lt;br /&gt;
*[[Fortunate number]]&lt;br /&gt;
*[[Harshad number]]&lt;br /&gt;
*[[Lucky number]]&lt;br /&gt;
*[[Perfect digital invariant]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
==Literature==&lt;br /&gt;
{{sfn whitelist |CITEREFSloane_&amp;quot;A055629&amp;quot;}}&lt;br /&gt;
* {{Cite book&lt;br /&gt;
 | last = Guy&lt;br /&gt;
 | first = Richard&lt;br /&gt;
 | author-link = Richard K. Guy&lt;br /&gt;
 | year = 2004&lt;br /&gt;
 | title = Unsolved Problems in Number Theory&lt;br /&gt;
 |edition=3rd&lt;br /&gt;
 | publisher = [[Springer-Verlag]]&lt;br /&gt;
 | isbn= 0-387-20860-7&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* Schneider, Walter: [https://web.archive.org/web/20060204094653/http://www.wschnei.de/digit-related-numbers/happy-numbers.html Mathews: Happy Numbers.]&lt;br /&gt;
* {{MathWorld|urlname=HappyNumber|title=Happy Number}}&lt;br /&gt;
* [http://nazgul04.ddns.net/happy/happy.php calculate if a number is happy] {{Webarchive|url=https://web.archive.org/web/20190125183821/http://nazgul04.ddns.net/happy/happy.php |date=25 January 2019 }}&lt;br /&gt;
* [http://mathforum.org/library/drmath/view/55856.html Happy Numbers] at The Math Forum.&lt;br /&gt;
* [https://web.archive.org/web/20180703133816/http://numberphile.com/videos/melancoil.html 145 and the Melancoil] at Numberphile.&lt;br /&gt;
* {{cite web|last=Symonds|first=Ria|title=7 and Happy Numbers|url=http://www.numberphile.com/videos/7happy.html|work=Numberphile|publisher=[[Brady Haran]]|access-date=2 April 2013|archive-url=https://web.archive.org/web/20180115215406/http://www.numberphile.com/videos/7happy.html|archive-date=15 January 2018|url-status=dead}}&lt;br /&gt;
&lt;br /&gt;
{{Classes of natural numbers}}&lt;br /&gt;
{{Prime number classes}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Happy Number}}&lt;br /&gt;
[[Category:Arithmetic dynamics]]&lt;br /&gt;
[[Category:Base-dependent integer sequences]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Mikeblas</name></author>
	</entry>
</feed>