A Python implementation to determine if a given integer is a "Happy Number".
A happy number is a number defined by the following process:
- Starting with any positive integer, replace the number by the sum of the squares of its digits.
- Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
- Those numbers for which this process ends in 1 are happy numbers.
- Python 3.6 or higher
You can use the happy_num function by importing it into your own Python scripts, or you can run the file directly to execute the built-in tests.
from happy_numbers import happy_num
# Check if 19 is a happy number
result = happy_num(19)
print(f"Is 19 a happy number? {result}") # Output: True
# Check if 123 is a happy number
result = happy_num(123)
print(f"Is 123 a happy number? {result}") # Output: FalseThe script includes simple assert statements at the bottom. To verify the logic is working correctly, simply run the script from your terminal:
python src/main.pyIf the script runs without outputting any errors, all tests have passed successfully.