forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
happy_number.py
48 lines (42 loc) · 1.38 KB
/
happy_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def is_happy_number(number: int) -> bool:
"""
A happy number is a number which eventually reaches 1 when replaced by the sum of
the square of each digit.
:param number: The number to check for happiness.
:return: True if the number is a happy number, False otherwise.
>>> is_happy_number(19)
True
>>> is_happy_number(2)
False
>>> is_happy_number(23)
True
>>> is_happy_number(1)
True
>>> is_happy_number(0)
Traceback (most recent call last):
...
ValueError: number=0 must be a positive integer
>>> is_happy_number(-19)
Traceback (most recent call last):
...
ValueError: number=-19 must be a positive integer
>>> is_happy_number(19.1)
Traceback (most recent call last):
...
ValueError: number=19.1 must be a positive integer
>>> is_happy_number("happy")
Traceback (most recent call last):
...
ValueError: number='happy' must be a positive integer
"""
if not isinstance(number, int) or number <= 0:
msg = f"{number=} must be a positive integer"
raise ValueError(msg)
seen = set()
while number != 1 and number not in seen:
seen.add(number)
number = sum(int(digit) ** 2 for digit in str(number))
return number == 1
if __name__ == "__main__":
import doctest
doctest.testmod()