Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions maths/special_numbers/happy_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def is_happy_number(number: int) -> bool:
"""
Check if a number is a happy number.
https://en.wikipedia.org/wiki/Happy_number
A happy number is defined by the following process:
1. Starting with any positive integer, replace the number by
the sum of the squares of its digits.
2. Repeat the process until the number equals 1 (happy) or
it loops endlessly in a cycle (not happy).

Args:
n (int): The number to check for happiness.

Returns:
bool: True if the number is a happy number, False otherwise.

Examples:
>>> is_happy_number(19)
True
>>> is_happy_number(4)
False
>>> is_happy_number(23)
True
"""

# Create a set to store seen numbers and detect cycles
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()